使用LINQ的Amazon Marketplace XML解析 [英] Amazon Marketplace XML parsing with LINQ

查看:52
本文介绍了使用LINQ的Amazon Marketplace XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列来自亚马逊的复杂XML文件,其中显示了订单报告.

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

XML片段如下:

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

以下代码将正确地捕获每个节点并将其放入列表项,但是我不确定如何在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,数量,价格等清单将变得更长,并阻止轻松循环.

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."}

代码是:

var query = from xEle in xmlDoc.Descendants(node)
            where xEle.Element("AmazonOrderID").Value.ToString() == primaryKey
            select new {
                   tag = xEle.Name.LocalName,
                   value = xEle.Value
            };

我不确定为什么会这样,node的变量和主键在运行时传递.

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:

Dictionary<string, string> ordersByID = new Dictionary<string, string>();

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

在解析CurNode时,出现空引用错误.

I get the null reference error as it parses CurNode.

推荐答案

您可以按照您的想法使用linq来实现,这样的事情应该可以工作,如果itemprice等需要的话,只需添加更多元素即可.命名空间)

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
            };

使用上面的代码,您可以单行取回每个亚马逊订单所需的所有信息...

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

这篇关于使用LINQ的Amazon Marketplace XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆