如何阅读和分析在C#中的XML文件? [英] How do I read and parse an XML file in C#?
本文介绍了如何阅读和分析在C#中的XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何阅读和分析在C#中的XML文件?
How do I read and parse an XML file in C#?
推荐答案
的XmlDocument从字符串或从文件中读取XML。
XmlDocument to read an XML from string or from file.
XmlDocument doc = new XmlDocument();
doc.Load("c:\\temp.xml");
或
doc.LoadXml("<xml>something</xml>");
然后在下面找到一个节点,它就是这样的。
then find a node below it ie like this
XmlNode node = doc.DocumentElement.SelectSingleNode("/book/title");
或
foreach(XmlNode node in doc.DocumentElement.ChildNodes){
string text = node.InnerText; //or loop through its children as well
}
然后阅读像这样的节点
then read the text inside that node like this
string text = node.InnerText;
或读取属性
string attr = node.Attributes["theattributename"]?.InnerText
请务必检查空的属性[东西],因为这将是NULL,如果属性不存在。
Always check for null on Attributes["something"] since it will be null if the attribute does not exist.
这篇关于如何阅读和分析在C#中的XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文