I have an XML file which returns a list of products with colors and inventory for each color. What is the best method to loop through this data only returning inventory by color for a specific product i.e. PL-0223?
Here is the data.
<Part fpartno="0019">
<Color colorname="Nickel">
<ValueAmt>
<Values Qty= "12101" Date ="ATP" Type= "Avail"/>
<Values Qty= "12101" Date= "Total" Type="Total"/>
</ValueAmt>
</Color>
</Part>
<Part fpartno="0223">
<Color colorname="White">
<ValueAmt>
<Values Qty= "0" Date ="ATP" Type= "Avail"/>
<Values Qty= "0" Date= "Total" Type="Total"/>
</ValueAmt>
</Color>
<Color colorname="Yellow">
<ValueAmt>
<Values Qty= "0" Date ="ATP" Type= "Avail"/>
<Values Qty= "0" Date= "Total" Type="Total"/>
</ValueAmt>
</Color>
</Part>
I've seen examples using both Linq to SQL and XmlReader but I have not found a good solution to loop through SubTree's/Child Nodes.
Examples of what I've tried.
XmlReader Method. I can't seem to figure out how to get the sub elements.
using(XmlReader r = XmlReader.Create(URLString))
{
while(r.Read())
{
if((r.NodeType == XmlNodeType.Element) && (r.Name == "Part"))
{
if(r.HasAttributes)
{
if(r.GetAttribute("fpartno") == "0019")
{
using (XmlReader cr = r.ReadSubtree())
{
Console.WriteLine(cr.Name)
}
}
}
}
}
}
I've also tried XDoc
XDocument xdoc = XDocument.Load(URLString);
foreach (XElement element in xdoc.Descendants("Values"))
{
Console.WriteLine(element);
}
But can't seem to figure out how to only get colors for "0019".