解析XML命名空间与C# [英] Parsing XML with Namespaces C#

查看:172
本文介绍了解析XML命名空间与C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力了一整天解析XML数据。好像我的问题是命名空间。我以为有一线希望我的最后网站的http://www.$c$cproject.com/Articles/30965/Read-XML-with-Namespace-resolution-using-XLinq-XEl但是失败也是如此。每次我运行code以下我得到未将对象引用设置到对象的实例。似乎有解析XML使用C#的方法很多,但这些名称空间痛死我了。我究竟做错了什么?

请记住我是新来的XML和C#和用来解析JSON数据。是的,我用Google搜索一些方法来解析XML数据并没有什么有在这种情况下是有用的。我可以不用XML命名空间的工作,但他们始终有一个错误。

C#code:

 的XElement元= XElement.Parse(XML);

 的XNamespace NS2 =htt​​p://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd;
 字符串VAL = element.Element(NS2 +ItemDimensions)元素(NS2 +Heigth)的价值。;
                                Console.WriteLine(VAL);
 

XML数据:

 < NS2:ItemAttributes中的XML:LANG =EN-US的xmlns:NS2 =htt​​p://mws.amazonservices.com/schema/Products/2011-10-01 /default.xsd">
  < NS2:作者> Troelsen,安德鲁< / NS2:作者>
  < NS2:绑定>平装< / NS2:绑定>
  < NS2:品牌>在preSS< / NS2:品牌>
  < NS2:版> 5℃/ NS2:版>
  < NS2:ItemDimensions>
    < NS2:高度单位=英寸> 9.21< / NS2:身高>
    < NS2:长度单位=英寸> 7.48; / NS2:长度>
    < NS2:宽度单位=英寸> 2.52< / NS2:宽>
    < NS2:重量单位=磅> 5.80< / NS2:重量>
  < / NS2:ItemDimensions>
  < NS2:IsAutographed>假< / NS2:IsAutographed>
  < NS2:IsEligibleForTradeIn>真< / NS2:IsEligibleForTradeIn>
  < NS2:IsMemorabilia>假< / NS2:IsMemorabilia>
  < NS2:标签> A preSS< / NS2:标签>
  < NS2:语言>
    < NS2:语言>
      < NS2:名称>英文LT; / NS2:名称>
      < NS2:型号>未知< / NS2:型号>
    < / NS2:语言>
    < NS2:语言>
      < NS2:名称>英文LT; / NS2:名称>
      < NS2:型号>原文和LT; / NS2:型号>
    < / NS2:语言>
    < NS2:语言>
      < NS2:名称>英文LT; / NS2:名称>
      < NS2:型号>发布< / NS2:型号>
    < / NS2:语言>
  < / NS2:语言>
  < NS2:ListPrice>
    < NS2:金额> 59.99< / NS2:金额>
    < NS2:货币code取代;美元< / NS2:货币code取代;
  < / NS2:ListPrice>
  < NS2:制造商>在preSS< / NS2:制造商>
  &所述; ns2的:NumberOfItems→1&其中; / NS2:NumberOfItems>
  < NS2:NumberOfPages> 1752< / NS2:NumberOfPages>
  < NS2:PackageDimensions>
    < NS2:高度单位=英寸> 2.60< / NS2:身高>
    < NS2:长度单位=英寸> 9.20< / NS2:长度>
    < NS2:宽度单位=英寸> 7.50< / NS2:宽>
    < NS2:重量单位=磅> 5.80< / NS2:重量>
  < / NS2:PackageDimensions>
  < NS2:型号> 9781430225492< / NS2:型号>
  < NS2:ProductGroup>图书< / NS2:ProductGroup>
  < NS2:ProductTypeName> ABIS_BOOK< / NS2:ProductTypeName>
  < NS2:PublicationDate> 2010-05-14< / NS2:PublicationDate>
  < NS2:出版者>在preSS< / NS2:出版社>
  < NS2:SmallImage>
    &LT; NS2:网址&GT; HTTP://ecx.images-amazon.com/images/I/51h9Sju5NKL._SL75_.jpg< / NS2:网址&GT;
    &LT; NS2:身高=单位像素&GT; 75℃/ NS2:身高&GT;
    &LT; NS2:宽=单位像素&GT; 61℃; / NS2:宽&GT;
  &LT; / NS2:SmallImage&GT;
  &LT; NS2:工作室&gt;在preSS&LT; / NS2:工作室&GT;
  &LT; NS2:标题&GT;临C#2010和.NET 4平台&LT; / NS2:标题&GT;
&LT; / NS2:ItemAttributes中&GT;
 

解决方案

首先:元素的名称是身高,而不是 Heigth

您可以使用此code获得身高值:

  VAR DOC = XDocument.Load(路径);

的XNamespace NS2 =htt​​p://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd;


字符串VAL = doc.Document.Descendants(NS2 +ItemDimensions)
    。.FirstOrDefault()元(NS2 +高度)的价值。

Console.WriteLine(VAL);
 

I have been working on parsing this XML data all day. It seems like my problem is namespaces. I thought a had a glimmer of hope with my last website http://www.codeproject.com/Articles/30965/Read-XML-with-Namespace-resolution-using-XLinq-XEl but it failed as well. Each time I run the code below I get "Object reference not set to an instance of an object." There seems to be many ways of parsing XML with C# but these namespaces are killing me. What am I doing wrong?

Do keep in mind I am new to XML and C# and use to parsing JSON data. And yes I have Googled several way to parse XML data and nothing has be useful in this case. I can get XML without the namespaces to work but with them always has that error.

C# Code:

 XElement element = XElement.Parse(xml);

 XNamespace ns2 = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd";
 string val = element.Element(ns2 + "ItemDimensions").Element(ns2 + "Heigth").Value;
                                Console.WriteLine(val);

XML Data:

<ns2:ItemAttributes xml:lang="en-US" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
  <ns2:Author>Troelsen, Andrew</ns2:Author>
  <ns2:Binding>Paperback</ns2:Binding>
  <ns2:Brand>Apress</ns2:Brand>
  <ns2:Edition>5</ns2:Edition>
  <ns2:ItemDimensions>
    <ns2:Height Units="inches">9.21</ns2:Height>
    <ns2:Length Units="inches">7.48</ns2:Length>
    <ns2:Width Units="inches">2.52</ns2:Width>
    <ns2:Weight Units="pounds">5.80</ns2:Weight>
  </ns2:ItemDimensions>
  <ns2:IsAutographed>false</ns2:IsAutographed>
  <ns2:IsEligibleForTradeIn>true</ns2:IsEligibleForTradeIn>
  <ns2:IsMemorabilia>false</ns2:IsMemorabilia>
  <ns2:Label>Apress</ns2:Label>
  <ns2:Languages>
    <ns2:Language>
      <ns2:Name>english</ns2:Name>
      <ns2:Type>Unknown</ns2:Type>
    </ns2:Language>
    <ns2:Language>
      <ns2:Name>english</ns2:Name>
      <ns2:Type>Original Language</ns2:Type>
    </ns2:Language>
    <ns2:Language>
      <ns2:Name>english</ns2:Name>
      <ns2:Type>Published</ns2:Type>
    </ns2:Language>
  </ns2:Languages>
  <ns2:ListPrice>
    <ns2:Amount>59.99</ns2:Amount>
    <ns2:CurrencyCode>USD</ns2:CurrencyCode>
  </ns2:ListPrice>
  <ns2:Manufacturer>Apress</ns2:Manufacturer>
  <ns2:NumberOfItems>1</ns2:NumberOfItems>
  <ns2:NumberOfPages>1752</ns2:NumberOfPages>
  <ns2:PackageDimensions>
    <ns2:Height Units="inches">2.60</ns2:Height>
    <ns2:Length Units="inches">9.20</ns2:Length>
    <ns2:Width Units="inches">7.50</ns2:Width>
    <ns2:Weight Units="pounds">5.80</ns2:Weight>
  </ns2:PackageDimensions>
  <ns2:PartNumber>9781430225492</ns2:PartNumber>
  <ns2:ProductGroup>Book</ns2:ProductGroup>
  <ns2:ProductTypeName>ABIS_BOOK</ns2:ProductTypeName>
  <ns2:PublicationDate>2010-05-14</ns2:PublicationDate>
  <ns2:Publisher>Apress</ns2:Publisher>
  <ns2:SmallImage>
    <ns2:URL>http://ecx.images-amazon.com/images/I/51h9Sju5NKL._SL75_.jpg</ns2:URL>
    <ns2:Height Units="pixels">75</ns2:Height>
    <ns2:Width Units="pixels">61</ns2:Width>
  </ns2:SmallImage>
  <ns2:Studio>Apress</ns2:Studio>
  <ns2:Title>Pro C# 2010 and the .NET 4 Platform</ns2:Title>
</ns2:ItemAttributes>

解决方案

Firstly: element name is Height and not Heigth.

You can use this code to get Height value:

var doc = XDocument.Load(path);

XNamespace ns2 = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd";


string val = doc.Document.Descendants(ns2 + "ItemDimensions")
    .FirstOrDefault().Element(ns2 + "Height").Value;

Console.WriteLine(val);

这篇关于解析XML命名空间与C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆