为什么这个 LINQ to XML 不起作用?
我从外部源收到此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不希望 doc.Descendants 返回 null - 它可能返回一个空序列,但这不会是“空值异常”。同样,
First()
- 它要么返回某物,要么抛出异常(虽然该“某物”可以是null
,但我不要期望后代会产生 null )。这样就剩下.Attribute("code").Value
。现在是的;.Attribute("code")
如果属性不存在,可以返回null
。如果您很高兴在这些情况下获得null
,我建议:转换运算符处理 null(不存在)属性。
I would not expect
doc.Descendants
to ever return anull
- 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 anull
, I do not expectDescendants
would be yieldingnull
s). So that leaves.Attribute("code").Value
. Now yes;.Attribute("code")
can return anull
, if an attribute doesn't exist. If you are happy to obtain anull
in those cases, I recommend:the conversion operator handles null (non-existent) attributes.
没有重现。你的代码基本没问题。
所以:调试。分解声明并检查内容。
doc
加载正常吗?No repro. Your code is basically OK.
So: Debug. Break the statement up and check things.
doc
loaded OK ?doc.Descendants(ns + "result").First()
deliver?检查
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.这个也测试过。最简单的测试方法是下载 LinqPad,创建新查询,然后从下拉列表中为语言选择“C# 语句”。单击“运行”,应该可以使用提供的代码。我还会按照“Rased Dot Net”的建议检查响应变量
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"