XML 根元素的值完全错误,未找到子节点

发布于 2024-12-23 00:10:17 字数 980 浏览 0 评论 0原文

我有一个非常基本的 XML 文件,如下所示:

<allData>
  <allDataDetails>
    <quoteid>ABC123</quoteid>
    <customername>John Smith</customername>
  </allDataDetails>
  <allDataDetails>
    <quoteid>DEF456</quoteid>
    <customername>Jane Doe</customername>
  </allDataDetails>
</allData>

我的 XSD 指定必须至少存在 1 个 allDataDetails 元素。该文档经过验证良好。

但是,当使用 Linq to XML 进行查询时,我似乎无法识别或查询 allData 中的内部元素。相反,当我在调试器中查看时,Value 属性是所有连接的数据。它看起来像这样:

ABC123John SmithDEF456Jane Doe

这是我的查询代码。 myRows 始终为空,因为我似乎无法获得后代:

XDocument entityXml = XDocument.Parse(myDataString);

var myRows = from d in entityXml.Descendants("allDataDetails")
             select new
             {
                quoteid = d.Element("quoteid").Value,
                customername = d.Element("customername").Value
             };

有人知道这里可能出了什么问题吗?

I have a very basic XML file that looks like this:

<allData>
  <allDataDetails>
    <quoteid>ABC123</quoteid>
    <customername>John Smith</customername>
  </allDataDetails>
  <allDataDetails>
    <quoteid>DEF456</quoteid>
    <customername>Jane Doe</customername>
  </allDataDetails>
</allData>

My XSD specifies that at least 1 allDataDetails element must exist. The doc is validated fine.

When querying using Linq to XML, though, I cannot seem to recognize or query for the inner elements within allData. Instead, when I view in the debugger, the Value attribute is all the data concatenated. It looks like this:

ABC123John SmithDEF456Jane Doe

Here's my query code. myRows is always null, because I cannot seem to get the descendants:

XDocument entityXml = XDocument.Parse(myDataString);

var myRows = from d in entityXml.Descendants("allDataDetails")
             select new
             {
                quoteid = d.Element("quoteid").Value,
                customername = d.Element("customername").Value
             };

Anyone know what could be wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-12-30 00:10:17

我的印象是您在本地窗口中查看了错误的变量 - 您正在查看整个 XDocument,并且它的 .Value 属性确实类似于所有节点值的串联字符串......

但是您确实应该查看 MyRows 变量 - 如果您确实正在执行您发布的代码,那么您应该看到这个:

在此处输入图像描述 (完整图片

这不是您正在寻找/期待的吗???

I have the impression you're looking at the wrong variable in your locals window - you're looking at the entire XDocument, and it's .Value property will indeed be something like a concatenated string of all node values.....

But you really should look at the MyRows variable - and if you're really executing the code you posted, you should see this:

enter image description here (full picture here)

Isnt' that what you're looking for / expecting???

铃予 2024-12-30 00:10:17

我刚刚运行了您的示例代码,并且修复了一些语法错误:
而且价值客户名称的逗号也是错误的,请尝试下面的我的版本,它应该可以工作。

string myDataString = @"<allData>
                          <allDataDetails>
                            <quoteid>ABC123</quoteid>
                            <customername>John Smith</customername>

                          </allDataDetails>
                          <allDataDetails>
                            <quoteid>DEF456</quoteid>
                            <customername>Jane Doe</customername>

                          </allDataDetails>
                        </allData>";
             XDocument entityXml = XDocument.Parse(myDataString);


             var myRows = from d in entityXml.Descendants("allDataDetails")
                      select new
                        {
                            quoteid = d.Element("quoteid").Value
                            ,customername = d.Element("customername").Value
                        };
             foreach (var rw in myRows)
                 Console.WriteLine(rw.customername + "\t" + rw.quoteid);

I just run your sample code, and few syntax error were fixed:
and also a comma for the value customer name was wrong, try my version below, it should work.

string myDataString = @"<allData>
                          <allDataDetails>
                            <quoteid>ABC123</quoteid>
                            <customername>John Smith</customername>

                          </allDataDetails>
                          <allDataDetails>
                            <quoteid>DEF456</quoteid>
                            <customername>Jane Doe</customername>

                          </allDataDetails>
                        </allData>";
             XDocument entityXml = XDocument.Parse(myDataString);


             var myRows = from d in entityXml.Descendants("allDataDetails")
                      select new
                        {
                            quoteid = d.Element("quoteid").Value
                            ,customername = d.Element("customername").Value
                        };
             foreach (var rw in myRows)
                 Console.WriteLine(rw.customername + "\t" + rw.quoteid);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文