[英]Parse XML in c# depending on nodes on XmlElement
我需要解析以下xml:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Items>
<Attr></Attr>
<Items>
<Goods>
<Attr></Attr>
<Attr></Attr>
<Attr></Attr>
</Goods>
</Response>
此xml存儲在XmlReader output
對象上。 是否有任何函數來獲取第一個節點元素Items並將其存儲在xmlelement1
這樣的xml元素對象中。 並解析/循環xmlelement1
並執行操作。
是的,使用XmlDocument:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("<<PathToXmlFileOnDisk>>");
或者如果您想加載XML字符串:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<<XmlText>>");
然后經歷一下:
foreach(XmlNode node in xmlDoc.SelectSingleNode("./Response").ChildNodes)
{
... //Once for each node under Response, then (Ex: Items has its own ChildNodes)
}
如果可以使用LINQ to XML:
XDocument doc = XDocument.Load(xml);
XElement xelem1 = doc.Root.Element("Items");
foreach (XElement elem in xelem1.Elements())
Console.WriteLine("Name: {0}, value: {1}", elem.Name, elem.Value);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.