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

    1. <legend id='I9NI0'><style id='I9NI0'><dir id='I9NI0'><q id='I9NI0'></q></dir></style></legend>

        • <bdo id='I9NI0'></bdo><ul id='I9NI0'></ul>

        将 xmlns 属性添加到我的 XML 文档中的根元素时,我的 linq 查询不起作用

        时间:2023-09-16

          <bdo id='UPEU7'></bdo><ul id='UPEU7'></ul>
          <tfoot id='UPEU7'></tfoot>

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

            <legend id='UPEU7'><style id='UPEU7'><dir id='UPEU7'><q id='UPEU7'></q></dir></style></legend>
              <tbody id='UPEU7'></tbody>

                1. <i id='UPEU7'><tr id='UPEU7'><dt id='UPEU7'><q id='UPEU7'><span id='UPEU7'><b id='UPEU7'><form id='UPEU7'><ins id='UPEU7'></ins><ul id='UPEU7'></ul><sub id='UPEU7'></sub></form><legend id='UPEU7'></legend><bdo id='UPEU7'><pre id='UPEU7'><center id='UPEU7'></center></pre></bdo></b><th id='UPEU7'></th></span></q></dt></tr></i><div id='UPEU7'><tfoot id='UPEU7'></tfoot><dl id='UPEU7'><fieldset id='UPEU7'></fieldset></dl></div>
                2. 本文介绍了将 xmlns 属性添加到我的 XML 文档中的根元素时,我的 linq 查询不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试更深入地了解 LINQ-to-XML,因此我为自己制作了一个简洁的 XML 文档小示例以进行尝试.此外,我尝试(并成功地)为该文件制作了自己的 XML 模式,只是为了进行测试.XML 文档非常简单,看起来像这样:

                  I'm trying to get more into LINQ-to-XML, so I've made myself a neat little example XML-document to try things out on. In addition, I tried (and successfully) made my own XML-schema for that file, just to test things out. The XML-document is pretty straightforward, and pretty much looks like this:

                  <cars xmlns="/carsSchema.xsd">
                    <car age="5">
                      <carId>1</carId>
                      <brand>BMW</brand>
                      <model>320i</model>
                      <color paintType="metallic">Red</color>
                    </car>
                  
                    <car age="2">
                      <carId>2</carId>
                      <brand>VW</brand>
                      <model>Golf</model>
                      <color paintType="matt">White</color>
                    </car>
                  [...]
                  </cars>
                  

                  现在,如果我从根元素中删除 xmlns-attribute,查询该文档就可以正常工作.当我重新添加它时,查询返回 null 并且什么也没有.我试图自己找出答案,但我还没有找到可以解决我的问题的解决方案.

                  Now, querying this document works just fine if i remove the xmlns-attribute from the root element. When i add it back in, the query returns null and nothing. I've tried to find out by myself, but I've yet to find a sollution that fixes my problem.

                  这是 C# 位:

                          XDocument xmlDoc = XDocument.Load(currentDir + "\Cars.xml");
                  
                  // XNamespace ns = "{" + currentDir + "\carSchema.xsd}";
                  // Tried to query xmlDoc.Descendants(ns+"car") after reading another post, 
                  // but that  made no difference
                  
                          var carInfo1 = from car in xmlDoc.Descendants("car")
                                         select (string)car.Element("brand") + ": " + (string)car.Element("model");
                  

                  有人知道出了什么问题吗?为什么 LINQ 真的要那么那么关心命名空间呢?它不能只查询我的文件而不关心它吗?

                  Anyone sees what's wrong? And why should LINQ really care that much about the namespace? Can't it just query my file and don't care about it?

                  提前致谢!:-)

                  推荐答案

                  按后代和元素搜索时,需要指定命名空间.使用 LINQ to XML 很容易做到这一点.看起来你快到了,但没有为元素这样做:

                  When you're searching by descendants and element, you need to specify the namespace. This is pretty easy with LINQ to XML. It looks like you were nearly there, but didn't do it for the elements:

                  XDocument xmlDoc = XDocument.Load(currentDir + "\Cars.xml");
                  // I don't think namespace URIs are really resolved. I'm not sure though -
                  // for a proof of concept, I suggest you use a namespace of
                  // http://dummy.com/dummy.xsd
                  XNamespace ns = "/carSchema.xsd";
                  
                  var carInfo1 = from car in xmlDoc.Descendants(ns + "car")
                                     select (string)car.Element(ns + "brand") + ": " + 
                                            (string)car.Element(ns + "model");
                  

                  这篇关于将 xmlns 属性添加到我的 XML 文档中的根元素时,我的 linq 查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C# 相当于 Java 中的通配符导入 下一篇:'System.Windows.Forms.FolderBrowserDialog' 是一种类型而不是命

                  相关文章

                  • <bdo id='AAHrH'></bdo><ul id='AAHrH'></ul>

                  1. <tfoot id='AAHrH'></tfoot>
                      <legend id='AAHrH'><style id='AAHrH'><dir id='AAHrH'><q id='AAHrH'></q></dir></style></legend>

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

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