[英]get attribute values from xml in c#
我正在使用 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 仍然为空值.. 任何建议和建议将不胜感激。
我怀疑问题是 - 您没有在这里处理属性。 考虑示例:
<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>
第一个片段依赖于attributes
而你的则将数据表示为values
。 因此,您可能需要像这样更改代码:
string r_marketplaceId = (string)xe.XPathSelectElement("//MarketplaceId").Value;
string r_ASIN = (string)xe.XPathSelectElement("//ASIN").Value;
希望这能解决你的问题
第一点,您正在声明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
我希望这会帮助你。
将 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());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.