[英]Parse XML document in C#
Duplicate: This is a duplicate of Best practices to parse xml files with C#? 重复:这是使用C#解析xml文件的最佳实践的副本? and many others (see https://stackoverflow.com/search?q=c%23+parse+xml ). 和许多其他人(见https://stackoverflow.com/search?q=c%23+parse+xml )。 Please close it and do not answer. 请关闭它,不要回答。
How do you parse XML document from bottom up in C#? 如何从C#自下而上解析XML文档?
For Example : 例如 :
<Employee>
<Name> Test </name>
<ID> 123 </ID>
<Employee>
<Company>
<Name>ABC</company>
<Email>test@ABC.com</Email>
</company>
Like these there are many nodes..I need to start parsing from bottom up like..first parse <company>
and then and so on..How doi go about this in C# ? 像这些有很多节点..我需要从下往上开始解析像...第一次解析<company>
然后依此类推。如何在C#中解决这个问题?
Try this: 试试这个:
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Path\To\Xml\File.xml");
Or alternatively if you have the XML in a string use the LoadXml
method. 或者,如果您在字符串中使用XML,请使用LoadXml
方法。
Once you have it loaded, you can use SelectNodes
and SelectSingleNode
to query specific values, for example: 加载后,可以使用SelectNodes
和SelectSingleNode
查询特定值,例如:
XmlNode node = doc.SelectSingleNode("//Company/Email/text()");
// node.Value contains "test@ABC.com"
Finally, note that your XML is invalid as it doesn't contain a single root node. 最后,请注意您的XML无效,因为它不包含单个根节点。 It must be something like this: 必须是这样的:
<Data>
<Employee>
<Name>Test</Name>
<ID>123</ID>
</Employee>
<Company>
<Name>ABC</Name>
<Email>test@ABC.com</Email>
</Company>
</Data>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.