method1
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);
}
}
C# 五种读取XML的方法
最新推荐文章于 2020-06-29 16:57:39 发布