C#读取XML节点内容方法实例入手让我们来看看C#读取XML节点的实现:
- using System;
- using System.Xml;
- using System.Xml.XPath;
- using System.Data;
- class ReadXML
- {
- public static void Main()
- {
- string sFile = "ReadXml.xml";
- //C#读取XML节点method 1
- XmlDocument doc = new XmlDocument();
- doc.Load(sFile);
- XmlNode node = doc.DocumentElement["News"]["Content"];
- Console.WriteLine(node.InnerText);
- //C#读取XML节点method2
- node = doc.SelectSingleNode("//Content");
- Console.WriteLine(node.InnerText);
- //similarly
- node = doc.DocumentElement.SelectSingleNode("News/Content");
- Console.WriteLine(node.InnerText);
- //C#读取XML节点method 3
- DataSet ds = new DataSet();
- ds.ReadXml(sFile);
- Console.WriteLine(ds.Tables[0].Rows[0]["Content"].ToString());
- //C#读取XML节点method 4
- XmlTextReader reader = new XmlTextReader(sFile);
- while (reader.Read())
- {
- if (reader.Name == "Content")
- {
- Console.WriteLine("***" + reader.ReadString());
- break;
- }
- }
- reader.Close();
- //C#读取XML节点method 5
- XPathDocument xpdoc = new XPathDocument(sFile);
- XPathNavigator xpnv = xpdoc.CreateNavigator();
- xpnv.MoveToFirstChild();
- xpnv.MoveToFirstChild();
- xpnv.MoveToFirstChild();
- xpnv.MoveToNext();xpnv.MoveToNext();xpnv.MoveToNext();
- Console.WriteLine("pathnavigator:" + xpnv.Value);
- }
- }
C#读取XML节点的实例讲解就到这里,希望对你了解学习C#读取XML节点有所帮助。
【编辑推荐】