[英]LINQ to XML GetDefaultNamespace() returns null
I'm trying to get the namespace from metadata in this xml document using XDocument: 我正在尝试使用XDocument从此xml文档中的元数据获取名称空间:
<?xml version="1.0" encoding="utf=8"?>
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>NugetName</id>
<version>1.0.0</version>
<authors>company</authors>
<owners>company</owners>
</metadata>
<files>
...
</files>
</package>
I've tried to use the GetDefaultNamespace() method from XElement like so: 我试图像这样从XElement使用GetDefaultNamespace()方法:
XNamespace ns = xmlDoc.Root.GetDefaultNamespace();
However I get a null value. 但是我得到一个空值。 I read a few comments that say that because the root namespace has multiple namespaces, it might not be possible to get the namespace of metadata using this method.
我读到一些评论说,由于根名称空间具有多个名称空间,因此使用此方法可能无法获得元数据的名称空间。
I thought to try just grabbing the Attribute from Root.Element("metadata") but because I don't have the namespace that wouldn't work I think. 我本想尝试从Root.Element(“ metadata”)抓取Attribute,但是因为我没有不起作用的命名空间,我认为。
Any ideas? 有任何想法吗?
PS the reason I won't necessarily know the namespace is because i'm consuming trusted nuspec files and i'm concerned the schema may change. PS我不一定知道名称空间的原因是因为我使用了受信任的nuspec文件,并且我担心架构可能会更改。
xmlDoc.Root
is the <package>
element. xmlDoc.Root
是<package>
元素。
It has no default namespace. 它没有默认名称空间。
You want xmlDoc.Root.Elements().First().GetDefaultNamespace()
. 您需要
xmlDoc.Root.Elements().First().GetDefaultNamespace()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.