XDocument.Root.Element返回null [英] XDocument.Root.Element returns null
本文介绍了XDocument.Root.Element返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有XML是这样的:
<?xml version="1.0" encoding="utf-16"?>
<RootNodeName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" MyAttribute="7" xmlns="mylink">
<IsValid>false</IsValid>
<Name>some matrix</Name>
...Some more nodes...
</RootNodeName>
和code至极是这样的:
and code wich is like:
var doc = XDocument.Parse(myXmlString);
Console.WriteLine(doc.Root.Element("Name"));
和控制台显示只是一个空的空间,因为 doc.Root.Element(名称)
返回空
= (
and console shows just an empty space since doc.Root.Element("Name")
returns null
=(
虽然我能找到的中 doc.Root.Elements()
结果这个元素。
doc.Root.Attribute(MyAttribute)
给出正确的结果也是如此。
While I can find this Element among doc.Root.Elements()
results.
doc.Root.Attribute("MyAttribute")
gives correct result as well.
有什么不对的地方/我?
What is wrong with it/me?
推荐答案
在&LT;名称&gt;
元素是在 myLink的
命名空间:
XNamespace mylink = "mylink";
Console.WriteLine(doc.Root.Element(mylink + "Name"));
这篇关于XDocument.Root.Element返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文