C# 利用system.xml 中的class讀取XML的方式:
要讀取的XML內容如下:
<
transaction>==>Root
<sqlcmd command="Insert into GlassEQPHistory(BCNo,LineID,NodeNo,EQName,Repdatetime,Funkey,Workid,WorkNo,Worktype,Pid,Groupflag,Eng_unit1,Eng_unit2,Overbakeflag,panel_angle ,criteria,Asmtype,Viewflag,Gapflag,Asmfinish,Sealfinish,Aufinish,RubbPass,SealPass,AUPass,LcdpPass,ChamberPass,Pair_no,NonMatchflg,RubxConfirmFlag,RubxPackFlag )values('7','RBAB0100','4','Rubx','20101218015044','Receive Data','F50CC011CL01','FA88','0','799','9','0','0','0','0','6','1','0','0','0','0','0','0','0','0','0','0','0','0','0','0')"/>
</
transaction>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace Csharp_Console_ReadXML
{
class Program
{
static void Main(string[] args)
{
// XmlTextReader xRD= new XmlTextReader("C:\\4_Receive Data_20101218015044.xml");
利用XMLDocument讀取;
XmlDocument xDL = new XmlDocument();
xDL.Load("C:\\4_Receive Data_20101218015044.xml"); //Load XML檔
讀取單一Node,利用SingleNode方法取得
XmlNode xSingleNode = xDL.SelectSingleNode("//sqlcmd");
XmlAttributeCollection xAT = xSingleNode.Attributes;
//讀出該Node所有的Attribute
for (int i = 0; i < xAT.Count; i++)
{
if (xAT.Item(i).Name == "command")
Console.WriteLine(xAT.Item(i).Value);
//讀出我們想要的attribute內容
}
'------------------這是分隔線--------------------------------------------------
若要讀取多個NodeList,使用SelectNodes方法
XmlNodeList NodeList = xDL.
SelectNodes("//sqlcmd");
foreach (XmlNode item_File in NodeList)
{
Console.WriteLine(item_File.Name);
XmlAttributeCollection xAT = item_File.Attributes;
for (int i = 0; i < xAT.Count; i++)
{
if (xAT.Item(i).Name == "command")
Console.WriteLine(xAT.Item(i).Value);
}
Console.WriteLine(item_File.InnerText);
}
Console.ReadLine();
}
}
}