Reputation: 698
I have an xmlString that I am parsing to an XDocument:
xmlString =
"<TestXml>" +
"<Data>" +
"<leadData>" +
"<Email>[email protected]</Email>" +
"<FirstName>John</FirstName>" +
"<LastName>Doe</LastName>" +
"<Phone>555-555-5555</Phone>" +
"<AddressLine1>123 Fake St</AddressLine1>" +
"<AddressLine2></AddressLine2>" +
"<City>Metropolis</City>" +
"<State>DC</State>" +
"<Zip>20016</Zip>" +
"</leadData>" +
"</Data>" +
"</TestXml>"
I parse the string to an XDocument, and then try and iterate through the nodes:
XDocument xDoc = XDocument.Parse(xmlString);
Dictionary<string, string> xDict = new Dictionary<string, string>();
//Convert xDocument to Dictionary
foreach (var child in xDoc.Root.Elements())
{
//xDict.Add();
}
This will only iterate once, and the one iteration seems to have all of the data in it. I realize I am doing something wrong, but after googling around I have no idea what.
Upvotes: 1
Views: 751
Reputation: 12546
Your root has only one child Data, therefore it iterates only once
var xDict = XDocument.Parse(xmlString)
.Descendants("leadData")
.Elements()
.ToDictionary(e => e.Name.LocalName, e => (string)e);
Upvotes: 0
Reputation: 414
Try xDoc.Root.Descendants()
instead of xDoc.Root.Elements()
in your foreach loop.
Upvotes: 2