为什么这个 LINQ to XML 不起作用?

发布于 2024-12-05 16:46:03 字数 789 浏览 1 评论 0原文

我从外部源收到此 XML:

<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
  <response>
    <result code="1300">
      <msg>Command completed successfully; no messages</msg>
    </result>
    <trID>
      <clTRID>TEST-12345</clTRID>
      <svTRID>IRNIC_2011-09-21T13:44:25+04:30_f1u</svTRID>
    </trID>
  </response>
</epp>

我想提取 结果元素code 属性。我使用了这段代码:

XDocument doc = XDocument.Parse(response);
XNamespace ns = "urn:ietf:params:xml:ns:epp-1.0";
string value = doc.Descendants(ns + "result").First().Attribute("code").Value;

但是,它会抛出空值异常,因为 doc.Descendants(ns + "result") 为空。

这是怎么回事?

I receive this XML from an external source:

<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
  <response>
    <result code="1300">
      <msg>Command completed successfully; no messages</msg>
    </result>
    <trID>
      <clTRID>TEST-12345</clTRID>
      <svTRID>IRNIC_2011-09-21T13:44:25+04:30_f1u</svTRID>
    </trID>
  </response>
</epp>

I want to extract the value of the code attribute of the result element. I used this code:

XDocument doc = XDocument.Parse(response);
XNamespace ns = "urn:ietf:params:xml:ns:epp-1.0";
string value = doc.Descendants(ns + "result").First().Attribute("code").Value;

However, it throws null value exception, because doc.Descendants(ns + "result") is null.

What's wrong here?

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

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

发布评论

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

评论(4

柏林苍穹下 2024-12-12 16:46:03

但是,它会抛出空值异常,因为 doc.Descendants(ns + "result") 为 null

我不希望 doc.Descendants 返回 null - 它可能返回一个空序列,但这不会是“空值异常”。同样,First() - 它要么返回某物,要么抛出异常(虽然该“某物”可以是null,但我不要期望后代会产生 null )。这样就剩下 .Attribute("code").Value。现在是的; .Attribute("code") 如果属性不存在,可以返回 null。如果您很高兴在这些情况下获得 null,我建议:

string value = (string)doc.Descendants(ns + "result").First().Attribute("code");

转换运算符处理 null(不存在)属性。

However, it throws null value exception, because doc.Descendants(ns + "result") is null

I would not expect doc.Descendants to ever return a null - it might return an empty sequence, but that wouldn't be a "null value exception". Likewise, First() - it either returns something, or it throws an exception (and while that "something" can be a null, I do not expect Descendants would be yielding nulls). So that leaves .Attribute("code").Value. Now yes; .Attribute("code") can return a null, if an attribute doesn't exist. If you are happy to obtain a null in those cases, I recommend:

string value = (string)doc.Descendants(ns + "result").First().Attribute("code");

the conversion operator handles null (non-existent) attributes.

堇年纸鸢 2024-12-12 16:46:03

没有重现。你的代码基本没问题。

所以:调试。分解声明并检查内容。

  • 您的 doc 加载正常吗?
  • doc.Descendants(ns + "result").First() 能提供什么?
  • ETC

No repro. Your code is basically OK.

So: Debug. Break the statement up and check things.

  • is your doc loaded OK ?
  • what does just doc.Descendants(ns + "result").First() deliver?
  • etc
上课铃就是安魂曲 2024-12-12 16:46:03

检查 response 变量,因为我从 xml 文件加载了 xml,并且您的代码运行良好。

在测试时,您可以将响应保存在 xml 文件中并从那里加载。

使用 XDocument.Load("a.xml") 加载 xml 文件。

Check response variable because i loaded the xml from an xml file and your code worked great.

In case of testing, you can save the response in an xml file and load it from there.

use XDocument.Load("a.xml") to load an xml file.

﹏雨一样淡蓝的深情 2024-12-12 16:46:03

这个也测试过。最简单的测试方法是下载 LinqPad,创建新查询,然后从下拉列表中为语言选择“C# 语句”。单击“运行”,应该可以使用提供的代码。我还会按照“Rased Dot Net”的建议检查响应变量

string xml =
@"<epp xmlns=""urn:ietf:params:xml:ns:epp-1.0"">
  <response>
    <result code=""1300"">
      <msg>Command completed successfully; no messages</msg>
    </result>
    <trID>
      <clTRID>TEST-12345</clTRID>
      <svTRID>IRNIC_2011-09-21T13:44:25+04:30_f1u</svTRID>
    </trID>
  </response>
</epp>";

XDocument doc = XDocument.Parse(xml);
Console.WriteLine(doc.ToString());
XNamespace ns = doc.Root.Name.Namespace;
Console.WriteLine("Namespace: " + ns.ToString());
string value = doc.Descendants(ns + "result").First().Attribute("code").Value;
Console.WriteLine(value);

Tested this too. Easiest way to test is to download LinqPad, create new query, then for Language select "C# statement(s)" from the dropdown. Click Run, should work with the code provided. I would also check the response variable as suggested by "Rased Dot Net"

string xml =
@"<epp xmlns=""urn:ietf:params:xml:ns:epp-1.0"">
  <response>
    <result code=""1300"">
      <msg>Command completed successfully; no messages</msg>
    </result>
    <trID>
      <clTRID>TEST-12345</clTRID>
      <svTRID>IRNIC_2011-09-21T13:44:25+04:30_f1u</svTRID>
    </trID>
  </response>
</epp>";

XDocument doc = XDocument.Parse(xml);
Console.WriteLine(doc.ToString());
XNamespace ns = doc.Root.Name.Namespace;
Console.WriteLine("Namespace: " + ns.ToString());
string value = doc.Descendants(ns + "result").First().Attribute("code").Value;
Console.WriteLine(value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文