简体   繁体   中英

Parse amazon mws api response xml Into a dataset with datatables

This is the repsonse I'm getting from amazon 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>

Does anyone have an efficient way to parse this into dataset with datatables?

Or is there official mws documentation on this?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM