I have a XML document that basically looks like this:
<Item>
<ItemID>1234567890</ItemID>
<ItemSpecifics>
<NameValueList>
<Name>Style</Name>
<Value>mens</Value>
</NameValueList>
<NameValueList>
<Name>UPC</Name>
<Value>123456789</Value>
</NameValueList>
</ItemSpecifics>
</Item>
And the "Item" Node repeats itself throughout the XML document, having multiple ItemID values and UPC values..
What I would like to do here is to extract the ItemID value from each Item node and it's UPC value.. So the output would be something like this (a list or dictionary containing):
ItemID UPC
1 598
2 2134
3 9999
And so on..
I have tried something like this:
var document = XDocument.Parse(xmlString);
var upcValues = document.Descendants("NameValueList")
.Where(pair => pair.Element("Name").Value == "UPC")
.Select(pair => pair.Element("Value").Value)
.DefaultIfEmpty("n/a");
But this doesn't really achieves what I want as I'm not really sure how to extract both ID and UPC value at the same time from document...
Can someone help me out ?
You need to start with Item
as that's the root of each entry that you want in the end. You also need to provide the namespace of the elements, which is defaulted in the root element of the doc. So something like:
XNamespace ns = "urn:ebay:apis:eBLBaseComponents";
var upcValues = document
.Descendants(ns + "Item")
.ToDictionary(
item => (string) item.Element(ns + "ItemID"),
item => (string) item.Elements(ns + "NameValueList")
.Single(nvl => (string) nvl.Element(ns + "Name") == "UPC")
.Element(ns + "Value"));
The first lambda expression selects the key in the dictionary element, from the ItemID
element. The second lambda expression selects the value in the dictionary element by finding the only NameValueList
element with a Name
element with a value of "NPC", then taking that NameValueList
's Value
element value.
See more on this question at Stackoverflow