C# XDocument.Root.Element返回null
我有XML,它类似于:C# XDocument.Root.Element返回null,c#,.net,xml,linq-to-xml,C#,.net,Xml,Linq To Xml,我有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=&q
<?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>
var doc = XDocument.Parse(myXmlString);
Console.WriteLine(doc.Root.Element("Name"));
控制台只显示一个空格,因为doc.Root.Element(“Name”)
返回null
=(
而我可以在doc.Root.Elements()
结果中找到这个元素。
doc.Root.Attribute(“MyAttribute”)
也给出了正确的结果
它/我有什么问题?元素位于mylink
命名空间中:
XNamespace mylink = "mylink";
Console.WriteLine(doc.Root.Element(mylink + "Name"));
太棒了!谢谢=)我应该更仔细地阅读文档=)如果它说:xmlns=“”
?谢谢