亚马逊商城XML解析亚马逊、商城、XML

2023-09-11 23:52:47 作者:逝水流年″轻染尘

我从亚马逊一系列复杂的XML文件显示订单报告。

I have a series of complicated XML files from Amazon showing order reports.

一个XML片段是如下:

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>

我需要做这个文件是提取XML文档的各个部分,然后做一些事情与数据。

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.

下面code将正确地抓住每一个节点,并把它变成一个列表项,但我不能确定如何使用C#中的相同顺序号的多个项目关联。

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#代码段:

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");

其中,XML是实例化类。

where xml is the instantiated class.

要进一步解释的并发症:如果每一个节点被放入其自己的列表中的列表的长度将是恒定提供只有一个项目是有序的。一旦多个项目正在有序的SKU,数量,价格等名单将变得更长和$ P​​ $ pvent一个简单的循环。

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.

我肯定有一个的LINQ to XML语句,可以做什么,我需要,但我没有在附近足够的经验与C#来破解它。

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 +++++++++++++++++++

我想我有各地的网络发现了一些LINQ的建议。下面看起来很有希望,但返回的异常:

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是:

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.

如果我设置断点,我可以看到的PrimaryKey被传递正确的,同节点;然而,当我到达:

If I set breakpoints, I can see that primaryKey is being passed correctly, same with node; however when I get to:

字典ordersByID =新词典();

Dictionary ordersByID = new Dictionary();

        foreach (var CurNode in query) {
            ordersByID.Add(CurNode.tag, CurNode.value);
        }

我得到的空引用错误,因为它解析CURNODE。

I get the null reference error as it parses CurNode.

推荐答案

您可以通过使用LINQ做到这一点,你想,像财产以后这应该工作,只是如果需要ITEMPRICE等加入更多的元素..:(其中Ns为命名空间)

You can achieve this by using linq as you thought, somthing like this should work, just add more elements if required for itemprice etc.. : (Where ns is the namespace)

xmlDoc = XDocument.Parse(sr.ReadToEnd());

XNamespace ns = "w3.org/2001/XMLSchema-instance";

var query = from order in xmlDoc.Descendants(ns + "Order")
            from orderItem in order.Elements(ns + "OrderItem")
            select new
            {        
                amazonOrdeID = order.Element(ns + "AmazonOrderID").Value,
                merchantOrderID = order.Element(ns + "MerchantOrderID ").Value,
                orderStatus = order.Element(ns + "OrderStatus ").Value,
                asin = orderItem.Element(ns + "ASIN").Value,
                quantity = orderItem.Element(ns + "quantity").Value
            };

使用上面的你shold能带回所有的单线每亚马逊为了你需要的信息...

using the above you shold be able to bring back all the information you need per amazon order in a singleline...