【问题标题】:get attribute values from xml in c#在c#中从xml获取属性值
【发布时间】:2020-06-16 12:12:18
【问题描述】:

我正在使用 C#,我想解析关系元素。
我想获得“MarketplaceId”(A1F83G8C2ARO7P)和“ASIN”(B076B1GP37)的价值

这是我的 XML。

   <Relationships>
     <VariationParent xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
      <Identifiers>
        <MarketplaceASIN>
          <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
          <ASIN>B076B1GP37</ASIN>
        </MarketplaceASIN>
      </Identifiers>
    </VariationParent>
  </Relationships>

到目前为止,这是我的代码。

if (relationshipList.IsSetAny())
{
   foreach(var relationship in relationshipList.Any)
   {
      string rxml = ProductsUtil.FormatXml((System.Xml.XmlElement)relationship);
      XDocument xDoc = XDocument.Parse(rxml);
      XNamespace ns = XNamespace.Get("http://mws.amazonservices.com/schema/Products/2011-10-01");

      IEnumerable<object> relationships = xDoc.Descendants();

      foreach (System.Xml.Linq.XElement xe in relationships)
      {
         string r_marketplaceId = (string)xe.Attribute("MarketplaceId");
         string r_ASIN = (string)xe.Attribute("ASIN");                                        
      }
    }
}

xe 上面是下面

 <VariationParent xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
  <Identifiers>
    <MarketplaceASIN>
      <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
      <ASIN>B076B1GP37</ASIN>
    </MarketplaceASIN>
  </Identifiers>
</VariationParent>

r_marketplaceId 和 r_ASIN 仍然是 Null 值。 任何意见和建议将不胜感激。

【问题讨论】:

    标签: c# xml parsing


    【解决方案1】:

    第一句话,你是在声明xn,不要使用它。

    您可以通过LINQ查询获取值,如以下代码:

    XNamespace xn = "http://mws.amazonservices.com/schema/Products/2011-10-01";
    
    IEnumerable<string> elements = XDocument.Parse(rxml)
        .Descendants(xn + "MarketplaceASIN")
        .Descendants()
        .Where(e=>!e.HasElements)
        .Select(element => element.Value);
    
    Console.WriteLine(string.Join(", ", elements));
    

    结果

    A1F83G8C2ARO7P, B076B1GP37

    希望对你有所帮助。

    【讨论】:

      【解决方案2】:

      我怀疑问题是 - 你在这里没有处理属性。考虑示例:

      <VariationParent xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
        <Identifiers>
          <MarketplaceASIN MarketplaceId="A1F83G8C2ARO7P" ASIN="B076B1GP37" /> <!-- these are attributes -->
        </Identifiers>
      </VariationParent>
      <!------------>
       <VariationParent xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
        <Identifiers>
          <MarketplaceASIN>
            <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId> <!-- these are values -->
            <ASIN>B076B1GP37</ASIN> <!-- these are values -->
          </MarketplaceASIN>
        </Identifiers>
      </VariationParent>
      

      第一个 sn-p 依赖于attributes,而你的将数据表示为values。 因此,您可能需要像这样更改代码:

      string r_marketplaceId = (string)xe.XPathSelectElement("//MarketplaceId").Value;
      string r_ASIN = (string)xe.XPathSelectElement("//ASIN").Value;
      

      希望这能解决您的问题

      【讨论】:

        【解决方案3】:

        使用带有字典的 xml linq。见下面的代码

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Xml;
        using System.Xml.Linq;
        
        namespace ConsoleApplication159
        {
            class Program
            {
                const string FILENAME = @"c:\temp\test.xml";
                static void Main(string[] args)
                {
                    XDocument doc = XDocument.Load(FILENAME);
                    XNamespace ns = doc.Root.GetDefaultNamespace();
                    Dictionary<string, string> dict1 = doc.Descendants(ns + "MarketplaceASIN")
                        .GroupBy(x => (string)x.Element(ns + "MarketplaceId"), y => (string)y.Element(ns + "ASIN"))
                        .ToDictionary(x => x.Key, y => y.FirstOrDefault());
                    //where MarketplaceId may be repeated
                    Dictionary<string, List<string>> dict2 = doc.Descendants(ns + "MarketplaceASIN")
                         .GroupBy(x => (string)x.Element(ns + "MarketplaceId"), y => (string)y.Element(ns + "ASIN"))
                        .ToDictionary(x => x.Key, y => y.ToList());
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-17
          • 1970-01-01
          • 2016-11-05
          • 1970-01-01
          • 2013-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多