2017-07-04 4 views
-1

まず、私の英語はすみません、私の母国語ではありません。だから私は基本的に私たちの電子商取引会社のためのWebアプリケーションを開発しています。 Amazon APIを使用してデータを取得しています。これらはXMLファイルにあります。以前はXMLで実際に作業したことがないので、私は学びたいと思っていますが、インターネット上で見つかったものは基本的なXML文書用です。深い2-3レベルのような簡単なforeachを使用して簡単に読み取ることができます。私はこの種のドキュメントにforeachを使用しようとしましたが、コードでは迷ってしまいました。このデータを使ってクラスのインスタンスを作成したいと思います。この例では、この製品には1つの製品(製品タグ)と10の製品(LowestOfferListingsタグ)しかありません。本当に複雑なXMLを読む(最大10レベル)

すべての要素を読み込んでインスタンスを作成するためにコード内で使用するものは何ですか?

私はC#を使用してい

つの応答(GetLowestOfferListingsForASINResponse)は、最大20の結果(GetLowestOfferListingsForASINResponse)

製品(プロダクト)がまたはオファー(LowestOfferListings)を有しても有さなくてもよいが、それを持つことができます確かに(識別子)を持っています。

P.S. GetLowestOfferListingsForASINResponseの終了タグは、下のコードブロックには表示されません。なぜ私は考えていない。しかし最後に終了タグがあります

ここにはamazon APIを使用した基本的な応答があります。

<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"> 
    <GetLowestOfferListingsForASINResult ASIN="ASIN_VALUE" status="Success"> 
    <AllOfferListingsConsidered>False</AllOfferListingsConsidered> 
    <Product> 
     <Identifiers> 
      <MarketplaceASIN> 
       <MarketplaceId>MARKETPLACE_ID_VALUE</MarketplaceId> 
       <ASIN>ASIN_VALUE</ASIN> 
      </MarketplaceASIN> 
     </Identifiers> 
     <LowestOfferListings> 
      <LowestOfferListing> 
       <Qualifiers> 
        <ItemCondition>New</ItemCondition> 
        <ItemSubcondition>New</ItemSubcondition> 
        <FulfillmentChannel>Merchant</FulfillmentChannel> 
        <ShipsDomestically>Unknown</ShipsDomestically> 
        <ShippingTime> 
         <Max>3-7 days</Max> 
        </ShippingTime> 
        <SellerPositiveFeedbackRating>80-89%</SellerPositiveFeedbackRating> 
       </Qualifiers> 
       <NumberOfOfferListingsConsidered>2</NumberOfOfferListingsConsidered> 
       <SellerFeedbackCount>190758</SellerFeedbackCount> 
       <Price> 
        <LandedPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>30.74</Amount> 
        </LandedPrice> 
        <ListingPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>30.74</Amount> 
        </ListingPrice> 
        <Shipping> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>0.00</Amount> 
        </Shipping> 
       </Price> 
       <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice> 
      </LowestOfferListing> 
      <LowestOfferListing> 
       <Qualifiers> 
        <ItemCondition>New</ItemCondition> 
        <ItemSubcondition>New</ItemSubcondition> 
        <FulfillmentChannel>Amazon</FulfillmentChannel> 
        <ShipsDomestically>True</ShipsDomestically> 
        <ShippingTime> 
         <Max>0-2 days</Max> 
        </ShippingTime> 
        <SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating> 
       </Qualifiers> 
       <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered> 
       <SellerFeedbackCount>226962</SellerFeedbackCount> 
       <Price> 
        <LandedPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>31.50</Amount> 
        </LandedPrice> 
        <ListingPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>31.50</Amount> 
        </ListingPrice> 
        <Shipping> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>0.00</Amount> 
        </Shipping> 
       </Price> 
       <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice> 
      </LowestOfferListing> 
      <LowestOfferListing> 
       <Qualifiers> 
        <ItemCondition>New</ItemCondition> 
        <ItemSubcondition>New</ItemSubcondition> 
        <FulfillmentChannel>Merchant</FulfillmentChannel> 
        <ShipsDomestically>Unknown</ShipsDomestically> 
        <ShippingTime> 
         <Max>0-2 days</Max> 
        </ShippingTime> 
        <SellerPositiveFeedbackRating>90-94%</SellerPositiveFeedbackRating> 
       </Qualifiers> 
       <NumberOfOfferListingsConsidered>3</NumberOfOfferListingsConsidered> 
       <SellerFeedbackCount>827887</SellerFeedbackCount> 
       <Price> 
        <LandedPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>31.68</Amount> 
        </LandedPrice> 
        <ListingPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>31.68</Amount> 
        </ListingPrice> 
        <Shipping> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>0.00</Amount> 
        </Shipping> 
       </Price> 
       <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice> 
      </LowestOfferListing> 
      <LowestOfferListing> 
       <Qualifiers> 
        <ItemCondition>New</ItemCondition> 
        <ItemSubcondition>New</ItemSubcondition> 
        <FulfillmentChannel>Merchant</FulfillmentChannel> 
        <ShipsDomestically>False</ShipsDomestically> 
        <ShippingTime> 
         <Max>3-7 days</Max> 
        </ShippingTime> 
        <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating> 
       </Qualifiers> 
       <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered> 
       <SellerFeedbackCount>9606</SellerFeedbackCount> 
       <Price> 
        <LandedPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>34.53</Amount> 
        </LandedPrice> 
        <ListingPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>30.54</Amount> 
        </ListingPrice> 
        <Shipping> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>3.99</Amount> 
        </Shipping> 
       </Price> 
       <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice> 
      </LowestOfferListing> 
      <LowestOfferListing> 
       <Qualifiers> 
        <ItemCondition>New</ItemCondition> 
        <ItemSubcondition>New</ItemSubcondition> 
        <FulfillmentChannel>Merchant</FulfillmentChannel> 
        <ShipsDomestically>Unknown</ShipsDomestically> 
        <ShippingTime> 
         <Max>0-2 days</Max> 
        </ShippingTime> 
        <SellerPositiveFeedbackRating>80-89%</SellerPositiveFeedbackRating> 
       </Qualifiers> 
       <NumberOfOfferListingsConsidered>2</NumberOfOfferListingsConsidered> 
       <SellerFeedbackCount>457149</SellerFeedbackCount> 
       <Price> 
        <LandedPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>40.04</Amount> 
        </LandedPrice> 
        <ListingPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>36.05</Amount> 
        </ListingPrice> 
        <Shipping> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>3.99</Amount> 
        </Shipping> 
       </Price> 
       <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice> 
      </LowestOfferListing> 
      <LowestOfferListing> 
       <Qualifiers> 
        <ItemCondition>New</ItemCondition> 
        <ItemSubcondition>New</ItemSubcondition> 
        <FulfillmentChannel>Merchant</FulfillmentChannel> 
        <ShipsDomestically>Unknown</ShipsDomestically> 
        <ShippingTime> 
         <Max>0-2 days</Max> 
        </ShippingTime> 
        <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating> 
       </Qualifiers> 
       <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered> 
       <SellerFeedbackCount>473643</SellerFeedbackCount> 
       <Price> 
        <LandedPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>40.05</Amount> 
        </LandedPrice> 
        <ListingPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>40.05</Amount> 
        </ListingPrice> 
        <Shipping> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>0.00</Amount> 
        </Shipping> 
       </Price> 
       <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice> 
      </LowestOfferListing> 
      <LowestOfferListing> 
       <Qualifiers> 
        <ItemCondition>New</ItemCondition> 
        <ItemSubcondition>New</ItemSubcondition> 
        <FulfillmentChannel>Merchant</FulfillmentChannel> 
        <ShipsDomestically>True</ShipsDomestically> 
        <ShippingTime> 
         <Max>0-2 days</Max> 
        </ShippingTime> 
        <SellerPositiveFeedbackRating>90-94%</SellerPositiveFeedbackRating> 
       </Qualifiers> 
       <NumberOfOfferListingsConsidered>2</NumberOfOfferListingsConsidered> 
       <SellerFeedbackCount>127</SellerFeedbackCount> 
       <Price> 
        <LandedPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>41.99</Amount> 
        </LandedPrice> 
        <ListingPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>38.00</Amount> 
        </ListingPrice> 
        <Shipping> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>3.99</Amount> 
        </Shipping> 
       </Price> 
       <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice> 
      </LowestOfferListing> 
      <LowestOfferListing> 
       <Qualifiers> 
        <ItemCondition>New</ItemCondition> 
        <ItemSubcondition>New</ItemSubcondition> 
        <FulfillmentChannel>Merchant</FulfillmentChannel> 
        <ShipsDomestically>Unknown</ShipsDomestically> 
        <ShippingTime> 
         <Max>3-7 days</Max> 
        </ShippingTime> 
        <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating> 
       </Qualifiers> 
       <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered> 
       <SellerFeedbackCount>1141</SellerFeedbackCount> 
       <Price> 
        <LandedPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>42.38</Amount> 
        </LandedPrice> 
        <ListingPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>38.39</Amount> 
        </ListingPrice> 
        <Shipping> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>3.99</Amount> 
        </Shipping> 
       </Price> 
       <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice> 
      </LowestOfferListing> 
      <LowestOfferListing> 
       <Qualifiers> 
        <ItemCondition>New</ItemCondition> 
        <ItemSubcondition>New</ItemSubcondition> 
        <FulfillmentChannel>Merchant</FulfillmentChannel> 
        <ShipsDomestically>True</ShipsDomestically> 
        <ShippingTime> 
         <Max>0-2 days</Max> 
        </ShippingTime> 
        <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating> 
       </Qualifiers> 
       <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered> 
       <SellerFeedbackCount>359758</SellerFeedbackCount> 
       <Price> 
        <LandedPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>43.09</Amount> 
        </LandedPrice> 
        <ListingPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>43.09</Amount> 
        </ListingPrice> 
        <Shipping> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>0.00</Amount> 
        </Shipping> 
       </Price> 
       <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice> 
      </LowestOfferListing> 
      <LowestOfferListing> 
       <Qualifiers> 
        <ItemCondition>New</ItemCondition> 
        <ItemSubcondition>New</ItemSubcondition> 
        <FulfillmentChannel>Merchant</FulfillmentChannel> 
        <ShipsDomestically>Unknown</ShipsDomestically> 
        <ShippingTime> 
         <Max>0-2 days</Max> 
        </ShippingTime> 
        <SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating> 
       </Qualifiers> 
       <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered> 
       <SellerFeedbackCount>5876</SellerFeedbackCount> 
       <Price> 
        <LandedPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>46.18</Amount> 
        </LandedPrice> 
        <ListingPrice> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>46.18</Amount> 
        </ListingPrice> 
        <Shipping> 
         <CurrencyCode>USD</CurrencyCode> 
         <Amount>0.00</Amount> 
        </Shipping> 
       </Price> 
       <MultipleOffersAtLowestPrice>Unknown</MultipleOffersAtLowestPrice> 
      </LowestOfferListing> 
     </LowestOfferListings> 
    </Product> 
</GetLowestOfferListingsForASINResult> 

答えて

1

私はあなたに完全な答えを与えることをしようとするつもり。 xmlを解析する方法はたくさんあります。最良の方法は、XMLファイル内のタグの数を含む多くの要素に依存します。多くのタグを含むamazonからの標準的なxml応答があります。この場合、カスタム解析を書くことは意味をなさない。

この場合、最良の方法は、アマゾンの既存の情報を使用することです。応答のための既存のスキーマが存在するのでだから、スキーマ

​​

))応答 https://docs.developer.amazonservices.com/en_UK/products/Products_GetLowestOfferListingsForASIN.html

2のドキュメントをアマゾンウェブページ

1を参照してくださいスキーマを取るとC#を作成するツールがありますクラス。 1つは、xmlデータの読み書きにxmlシリアル化/逆シリアル化を使用できるクラスがあります。

MSDNのツールを参照しています。ここhttps://docs.microsoft.com/en-us/dotnet/framework/serialization/xml-schema-definition-tool-xsd-exe

は、私がここでXML

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Xml; 
using System.Xml.Serialization; 
using System.Xml.Schema; 
using System.IO; 

namespace XMLSerialize 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      AppConfig config = new AppConfig(); 
      config.Charts = new List<ChartGeneric>(); 
      Pie newPie = new Pie(); 
      config.Charts.Add(newPie); 
      newPie.Attribute3 = "abc"; 

      StackedBar newStack = new StackedBar(); 
      config.Charts.Add(newStack); 
      newStack.Attribute1 = "ghi"; 
      newStack.Attribute2 = "jkl"; 

      XmlSerializer serializer = new XmlSerializer(typeof(AppConfig)); 

      StreamWriter writer = new StreamWriter(FILENAME); 
      serializer.Serialize(writer, config); 
      writer.Flush(); 
      writer.Close(); 
      writer.Dispose(); 


      GetConfig(FILENAME); 
     } 
     public static AppConfig GetConfig(String filepath) 
     { 
      XmlSerializer xs = new XmlSerializer(typeof(AppConfig)); 
      XmlTextReader reader = new XmlTextReader(filepath); 
      AppConfig configData = (AppConfig)xs.Deserialize(reader); 
      return configData; 
     } 
    } 
    [Serializable, XmlRoot("Configuration")] 
    public class AppConfig 
    { 

     [XmlArray("Charts")] 
     public List<ChartGeneric> Charts; 

     [XmlElement("AnotherElement", Form = XmlSchemaForm.Unqualified)] 
     public XmlElement[] AnotherElement { get; set; } 

     [XmlElement("OneElement", Form = XmlSchemaForm.Unqualified)] 
     public XmlElement[] OneElement { get; set; } 
    } 

    [XmlInclude(typeof(Pie))] 
    [XmlInclude(typeof(StackedBar))] 
    [Serializable] 
    public class ChartGeneric 
    { 
     public string Attribute1 { get; set; } 

     public string Attribute2 { get; set; } 
    } 
    [Serializable] 
    [XmlRoot(ElementName = "Pie")] 
    public class Pie : ChartGeneric 
    { 
     [XmlAttribute] 
     public string Attribute3 { get; set; } 
    } 

    [Serializable] 
    [XmlRoot(ElementName = "StackedBar")] 
    public class StackedBar : ChartGeneric 
    { 
     [XmlAttribute] 
     public string Attribute4 { get; set; } 
    } 

} 

をシリアライズとデシリアライズするために書いたサンプルコードは実際のXMLのためのソリューション

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Serialization; 
using System.IO; 

namespace ConsoleApplication64 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      string xml = File.ReadAllText(FILENAME); 
      StringReader reader = new StringReader(xml); 

      XmlSerializer xs = new XmlSerializer(typeof(GetLowestOfferListingsForASINResponse)); 
      GetLowestOfferListingsForASINResponse response = (GetLowestOfferListingsForASINResponse)xs.Deserialize(reader); 
     } 
    } 
    [XmlRoot(ElementName = "GetLowestOfferListingsForASINResponse", Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01")] 
    public class GetLowestOfferListingsForASINResponse 
    { 
     [XmlElement("GetLowestOfferListingsForASINResult")] 
     public List<GetLowestOfferListingsForASINResult> getLowestOfferListingsForASINResult { get; set; } 
    } 
    [XmlRoot(ElementName = "GetLowestOfferListingsForASINResult", Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01")] 
    public class GetLowestOfferListingsForASINResult 
    { 
     [XmlAttribute("ASIN")] 
     public string ASIN { get; set;} 
     [XmlAttribute("status")] 
     public string status { get; set; } 

     [XmlElement("AllOfferListingsConsidered")] 
     public string AllOfferListingsConsidered { get; set; } 

     [XmlElement("Product")] 
     public Product product { get; set; } 
    } 
    [XmlRoot(ElementName = "Product", Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01")] 
    public class Product 
    { 
     [XmlElement("Identifiers")] 
     public Identifiers identifiers { get; set; } 
    } 
    [XmlRoot(ElementName = "Identifiers", Namespace = "http://mws.amazonservices.com/schema/Products/2011-10-01")] 
    public class Identifiers 
    { 
    } 
} 
+0

そのコード簡略化されていますきれいに見えます。ご回答有難うございます。私はシリアライゼーション/デシリアライゼーションを調べます。 基本的には、amazonで指定されたスキームを使用してクラスを作成し、xmlデータをこれらのクラスにシリアル化する必要があります。 – Gorkem

+0

はい、msdnツールを使用します。コード内のクラスとしてスキーマにない3つのタグ(最初は応答3)を追加する必要があります。 – jdweng

+0

私はいくつかの結果を何時間も得ようとしています。 XMLは本当に複雑です。私はamazonによって与えられたxsdを使ってクラスを作成しました。逆シリアル化しようとしていますが、コンソールウィンドウに{ '" は期待されていませんでした "というメッセージが表示され続ける"} いくつかの解決方法があります。これはちょっとイライラしています – Gorkem

関連する問題