I need to parse the following xml :
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Items>
<Attr></Attr>
<Items>
<Goods>
<Attr></Attr>
<Attr></Attr>
<Attr></Attr>
</Goods>
</Response>
This xml is stored on XmlReader output
object . Is there any Function to get the first node element Items and store it in an xml element object say xmlelement1
. And parse/loop through the xmlelement1
and perform actions.
Yep, use the XmlDocument:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("<<PathToXmlFileOnDisk>>");
or if you want to load a string of XML:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<<XmlText>>");
Then go through the thing:
foreach(XmlNode node in xmlDoc.SelectSingleNode("./Response").ChildNodes)
{
... //Once for each node under Response, then (Ex: Items has its own ChildNodes)
}
If you can use 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.