Reputation: 1911
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.
Upvotes: 0
Views: 96
Reputation: 11228
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);
Upvotes: 1
Reputation: 2975
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)
}
Upvotes: 1