0

这是我从亚马逊 mws ListInventorySupplyRequest 得到的回复

<ListInventorySupplyResponse xmlns="http://mws.amazonaws.com/FulfillmentInventory/2010-10-01/">
  <ListInventorySupplyResult>
    <InventorySupplyList>
      <member>
        <SellerSKU>SKIjkhsad </SellerSKU>
        <FNSKU>lksdkl</FNSKU>
        <ASIN>;kzjsdkj</ASIN>
        <Condition>NewItem</Condition>
        <TotalSupplyQuantity>0</TotalSupplyQuantity>
        <InStockSupplyQuantity>0</InStockSupplyQuantity>
        <SupplyDetail />
      </member>
      <member>
        <SellerSKU>another sku</SellerSKU>
        <FNSKU>dklhfa</FNSKU>
        <ASIN>ajshdf;a</ASIN>
        <Condition>NewItem</Condition>
        <TotalSupplyQuantity>8</TotalSupplyQuantity>
        <InStockSupplyQuantity>8</InStockSupplyQuantity>
        <EarliestAvailability>
          <TimepointType>Immediately</TimepointType>
        </EarliestAvailability>
        <SupplyDetail>
          <member>
            <Quantity>8</Quantity>
            <SupplyType>InStock</SupplyType>
            <EarliestAvailableToPick>
              <TimepointType>Immediately</TimepointType>
            </EarliestAvailableToPick>
            <LatestAvailableToPick>
              <TimepointType>Immediately</TimepointType>
            </LatestAvailableToPick>
          </member>
        </SupplyDetail>
      </member>
    </InventorySupplyList>
    <NextToken>nextTken</NextToken>
  </ListInventorySupplyResult>
  <ResponseMetadata>
    <RequestId>RequestID</RequestId>
  </ResponseMetadata>
</ListInventorySupplyResponse>

有没有人有一种有效的方法将其解析为带有数据表的数据集?

或者是否有关于此的官方 mws 文档?

4

1 回答 1

0

In general, you could use the following to do what you want to do:

using System.Data;

DataSet data = new DataSet();
data.ReadXml(@"Example.xml");

The problem with the XML you posted is that it has nested elements that are named the same. Because of this, you will get an error during the data.ReadXml(). You should be able to solve this problem by using an XML transformation with the following code:

using System.Data;
using System.Xml.Xsl;

XslTransform transform = new XslTransform();
transform.Load(@"Transform.xsl");
transform.Transform(@"Example.xml", @"TransformedExample.xml");

DataSet data = new DataSet();
data.ReadXml(@"TransformedExample.xml");

Unfortunately I have not written an XML transformations in quite a few years and I don't have the time to research the syntax for that right now. If this question is not answered later tonight when I have some more time, I can research XML transformations and post an example transformation for you.

Edited to Add: For testing purposes, if you remove the element inside of the element of the example you posted, data.ReadXml() can read the example XML into the DataSet.

Edited to Add: I finally had some time to create an example XML transformation for you. As I mentioned above, it has been quite a few years since I have written any XSL. This should work for your example, but it may need to be modified to work with your actual XML document. What this transformation is does is rename the InventorySupplyList/member child element and the SupplyDetail/member child element to InventorySupplyListMember and SupplyDetailMember respectively.

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:aws="http://mws.amazonaws.com/FulfillmentInventory/2010-10-01/"
    xsl:exclude-result-prefixes="aws">

    <xsl:output indent="yes"/>

    <!--Identity Transform.-->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="aws:InventorySupplyList/aws:member">
        <xsl:element name="InventorySupplyListMember" namespace="{namespace-uri()}">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="aws:SupplyDetail/aws:member">
        <xsl:element name="SupplyDetailMember" namespace="{namespace-uri()}">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
于 2015-03-10T17:22:25.293 回答