<bdo id='VsibR'></bdo><ul id='VsibR'></ul>
  • <tfoot id='VsibR'></tfoot>

    <small id='VsibR'></small><noframes id='VsibR'>

    <legend id='VsibR'><style id='VsibR'><dir id='VsibR'><q id='VsibR'></q></dir></style></legend>

      <i id='VsibR'><tr id='VsibR'><dt id='VsibR'><q id='VsibR'><span id='VsibR'><b id='VsibR'><form id='VsibR'><ins id='VsibR'></ins><ul id='VsibR'></ul><sub id='VsibR'></sub></form><legend id='VsibR'></legend><bdo id='VsibR'><pre id='VsibR'><center id='VsibR'></center></pre></bdo></b><th id='VsibR'></th></span></q></dt></tr></i><div id='VsibR'><tfoot id='VsibR'></tfoot><dl id='VsibR'><fieldset id='VsibR'></fieldset></dl></div>

      1. 使用命名空间 C# 解析 XML

        时间:2023-08-26
          <tbody id='3ZcqA'></tbody>

        <i id='3ZcqA'><tr id='3ZcqA'><dt id='3ZcqA'><q id='3ZcqA'><span id='3ZcqA'><b id='3ZcqA'><form id='3ZcqA'><ins id='3ZcqA'></ins><ul id='3ZcqA'></ul><sub id='3ZcqA'></sub></form><legend id='3ZcqA'></legend><bdo id='3ZcqA'><pre id='3ZcqA'><center id='3ZcqA'></center></pre></bdo></b><th id='3ZcqA'></th></span></q></dt></tr></i><div id='3ZcqA'><tfoot id='3ZcqA'></tfoot><dl id='3ZcqA'><fieldset id='3ZcqA'></fieldset></dl></div>
        • <tfoot id='3ZcqA'></tfoot>
          <legend id='3ZcqA'><style id='3ZcqA'><dir id='3ZcqA'><q id='3ZcqA'></q></dir></style></legend>

          <small id='3ZcqA'></small><noframes id='3ZcqA'>

          • <bdo id='3ZcqA'></bdo><ul id='3ZcqA'></ul>

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

                  问题描述

                  我整天都在解析这个 XML 数据.看来我的问题是名称空间.我认为我的上一个网站有一线希望 http://www.codeproject.com/Articles/30965/Read-XML-with-Namespace-resolution-using-XLinq-XEl 但它也失败了.每次我运行下面的代码时,我都会得到对象引用未设置为对象的实例".似乎有很多用 C# 解析 XML 的方法,但是这些命名空间让我很生气.我做错了什么?

                  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?

                  请记住,我是 XML 和 C# 的新手,并且习惯于解析 JSON 数据.是的,我用谷歌搜索了几种解析 XML 数据的方法,在这种情况下没有任何用处.我可以在没有命名空间的情况下获取 XML,但使用它们总是会出现错误.

                  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#代码:

                   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 数据:

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

                  推荐答案

                  首先:元素名称是Height而不是Heigth.

                  Firstly: element name is Height and not Heigth.

                  您可以使用此代码获取 Height 值:

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

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

                  上一篇:删除 XML 节点 下一篇:单遍读取和验证 XML 与 C# 中引用的 XSD

                  相关文章

                  最新文章

                  1. <i id='cquQv'><tr id='cquQv'><dt id='cquQv'><q id='cquQv'><span id='cquQv'><b id='cquQv'><form id='cquQv'><ins id='cquQv'></ins><ul id='cquQv'></ul><sub id='cquQv'></sub></form><legend id='cquQv'></legend><bdo id='cquQv'><pre id='cquQv'><center id='cquQv'></center></pre></bdo></b><th id='cquQv'></th></span></q></dt></tr></i><div id='cquQv'><tfoot id='cquQv'></tfoot><dl id='cquQv'><fieldset id='cquQv'></fieldset></dl></div>
                      <bdo id='cquQv'></bdo><ul id='cquQv'></ul>
                    1. <legend id='cquQv'><style id='cquQv'><dir id='cquQv'><q id='cquQv'></q></dir></style></legend>
                    2. <tfoot id='cquQv'></tfoot>

                      <small id='cquQv'></small><noframes id='cquQv'>