简体   繁体   中英

get attribute values from xml in c#

I'm using C# and I want to parse the Relationship elements.
I want to get value of "MarketplaceId"(A1F83G8C2ARO7P) and "ASIN" (B076B1GP37)

Here is my 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>

And Here is my code so far.

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");                                        
      }
    }
}

Above xe is below

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

r_marketplaceId and r_ASIN still Null value.. any advice and suggestions will be greatly appreciated.

I suspect the issue is - you're not dealing with attributes here. Consider example:

<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>

first snippet is relying on attributes while yours expresses the data as values . Therefore you probably need to change your code like so:

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

hopefully that's resolves your issue

The first remark, you are declaring xn and don't use it.

You can get the values by LINQ query, like the following code :

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));

Result

A1F83G8C2ARO7P, B076B1GP37

I hope that will help you out.

Use xml linq with a dictionary. See code below

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());
        }
    }
}

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