C#读取XML节点内容方法
using System;
using System.Xml;
using System.Xml.XPath;
using System.Data;
class ReadXML
{
public static void Main()
{
string sFile = "ReadXml.xml";
//method 1
XmlDocument doc = new XmlDocument();
doc.Load(sFile);
XmlNode node = doc.DocumentElement["News"]["Content"];
Console.WriteLine(node.InnerText);
//method2
node = doc.SelectSingleNode("//Content");
Console.WriteLine(node.InnerText);
//similarly
node = doc.DocumentElement.SelectSingleNode("News/Content");
Console.WriteLine(node.InnerText);
//method 3
DataSet ds = new DataSet();
ds.ReadXml(sFile);
Console.WriteLine(ds.Tables[0].Rows[0]["Content"].ToString());
//method 4
XmlTextReader reader = new XmlTextReader(sFile);
while (reader.Read())
{
if (reader.Name == "Content")
{
Console.WriteLine("***" + reader.ReadString());
break;
}
}
reader.Close();
//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);
}
}
using System.Xml;
using System.Xml.XPath;
using System.Data;
class ReadXML
{
public static void Main()
{
string sFile = "ReadXml.xml";
//method 1
XmlDocument doc = new XmlDocument();
doc.Load(sFile);
XmlNode node = doc.DocumentElement["News"]["Content"];
Console.WriteLine(node.InnerText);
//method2
node = doc.SelectSingleNode("//Content");
Console.WriteLine(node.InnerText);
//similarly
node = doc.DocumentElement.SelectSingleNode("News/Content");
Console.WriteLine(node.InnerText);
//method 3
DataSet ds = new DataSet();
ds.ReadXml(sFile);
Console.WriteLine(ds.Tables[0].Rows[0]["Content"].ToString());
//method 4
XmlTextReader reader = new XmlTextReader(sFile);
while (reader.Read())
{
if (reader.Name == "Content")
{
Console.WriteLine("***" + reader.ReadString());
break;
}
}
reader.Close();
//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);
}
}