using System.Xml;

XmlDocument xd = new XmlDocument();

//Load裡放xml的路徑就可去讀取
xd.Load("ABC.xml");

//可將xml裡的<Flash>元素都存到nodelist裡
XmlNodeList nodelist = xd.SelectNodes("//Flash");

//再用迴圈去跑出來
for (int i = 0; i < nodelist.Count; i++)
{
//去讀第一個<Flash>元素裡的<XML_Sample>元素裡的內容
//InnerText就是讀元素裡的內容<Flash>我是內容</Flash>
//而InnerXml是讀元素裡的所有XML的樣子(包含元素,屬性,內容)
//另外這裡是用SelectSingleNode去讀單一個Node
//而上面的是用SelectNodes去讀多個Node
StreamReader sr = new StreamReader(nodelist[i].SelectSingleNode("XML_Sample").InnerText, Encoding.UTF8);

StringBuilder mysb = new StringBuilder();

while (sr.Peek() != -1)
{
mysb.Append(sr.ReadLine());
mysb.Append(Environment.NewLine);
}



sr.Dispose();

}

 

另外讀屬性的方法,File是Node,FileName是屬性

 XmlNodeList NodeList = xd.SelectNodes("//File");

                foreach (XmlNode item_File in NodeList)
                {
                    if (item_File.Attributes.GetNamedItem("FileName").Value == FileName)
                    {
                        XmlNodeList MappingList= item_File.ChildNodes;

                        foreach (XmlNode item_Mapping in MappingList)
                        {
                           //DoSomeThings
                        }
                    }                
                }

welkingunther 發表在 痞客邦 PIXNET 留言(0) 人氣()