I have a series of complicated XML files from Amazon showing order reports.
A XML snippet is as follow:
<Order>
<AmazonOrderID>000-1111111-2222222</AmazonOrderID>
<MerchantOrderID>111-3333333-4444444</MerchantOrderID>
<PurchaseDate>2012-03-02T13:28:53+00:00</PurchaseDate>
<LastUpdatedDate>2012-03-02T13:29:05+00:00</LastUpdatedDate>
<OrderStatus>Pending</OrderStatus>
<SalesChannel>Amazon.com</SalesChannel>
<URL>http://www.amazon.com</URL>
<FulfillmentData>
<FulfillmentChannel>Amazon</FulfillmentChannel>
<ShipServiceLevel>Standard</ShipServiceLevel>
<Address>
<City>Beverly Hills</City>
<State>CA</State>
<PostalCode>90210-1234</PostalCode>
<Country>US</Country>
</Address>
</FulfillmentData>
<OrderItem>
<ASIN>AmazonASIN </ASIN>
<SKU> Internal-SKU</SKU>
<ItemStatus>Pending</ItemStatus>
<ProductName> This is the name of the product </ProductName>
<Quantity>1</Quantity>
<ItemPrice>
<Component>
<Type>Principal</Type>
<Amount currency="USD">19.99</Amount>
</Component>
</ItemPrice>
</OrderItem>
</Order>
What I need to do with this file is extract various parts of the XML document and then do a number of things with the data.
The problem I am having comes with multiple order items.
The following code will correctly grab each node and put it into a list item, however I am unsure how to associate those multiple items with the same order number within C#.
C# snippet:
List<string> getNodes(string path, string nodeName) {
List<string> nodes = new List<string>();
XDocument xmlDoc = XDocument.Load(path); //Create the XML document type
foreach (var el in xmlDoc.Descendants(nodeName)) {
//for debugging
//nodes.Add(el.Name + " " + el.Value);
//for production
nodes.Add(el.Value);
}
return nodes;
} //end getNodes
The method is called like:
List<string> skuNodes = xml.getNodes(@"AmazonSalesOrders.xml", "SKU");
where xml is the instantiated class.
To further explain the complication: If each node is put into its own list the length of the list will be constant providing only one item is ordered. Once multiple items are ordered the SKU, quantity, price etc lists will become longer and prevent an easy loop.
I am sure there is a LINQ to XML statement that can do what I need, but I am no where near experienced enough with C# to hack it out.
+++++++++++++++ EDIT +++++++++++++++++++
I'm trying some LINQ suggestions I have found around the web. The following looks promising but is returning exception:
base {System.SystemException} = {"Object reference not set to an instance of an object."}
code is:
var query = from xEle in xmlDoc.Descendants(node)
where xEle.Element("AmazonOrderID").Value.ToString() == primaryKey
select new {
tag = xEle.Name.LocalName,
value = xEle.Value
};
I am unsure why this is occurring, the variables of node, and primary key are passed at runtime.
If I set breakpoints, I can see that primaryKey is being passed correctly, same with node; however when I get to:
Dictionary<string, string> ordersByID = new Dictionary<string, string>();
foreach (var CurNode in query) {
ordersByID.Add(CurNode.tag, CurNode.value);
}
I get the null reference error as it parses CurNode.