阅读非常复杂的XML(最多10级)

时间:2017-07-04 23:03:03

标签: c# asp.net xml

首先,抱歉我的英语,这不是我的第一语言。所以基本上我正在为我们的电子商务公司开发一个Web应用程序。我使用Amazon API来获取数据。这些都是XML文件。我之前从未真正使用过XML,所以我一直在努力学习,但我在互联网上发现的一切都是基本的xml文档。像2-3级深,可以使用简单的foreach轻松阅读。我曾尝试将foreach用于此类文档,但我在代码中迷失了方向。我想通过使用此数据创建一个类的实例。此示例只有一个产品(产品标签)和10个产品(LowestOfferListings标签)。

我应该在代码中使用什么来读取每个元素并创建实例?

我正在使用 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 个答案:

答案 0 :(得分:1)

我会尽力给你完整的答案。有很多方法可以解析xml。最好的方法取决于很多因素,包括xml文件中的标签数量。您有来自amazon的标准xml响应,其中包含大量标记。在这种情况下,编写自定义解析并没有多大意义。

在这种情况下,最好的方法是使用亚马逊的现有信息。所以请看亚马逊网页

1)回复文件 https://docs.developer.amazonservices.com/en_UK/products/Products_GetLowestOfferListingsForASIN.html

2)架构

http://g-ecx.images-amazon.com/images/G/01/mwsportal/doc/en_US/products/ProductsAPI_Response.xsd

由于存在响应的现有模式,因此有一些工具将采用模式并创建c#类。您可以使用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
    {
    }
}