简体   繁体   English

在 XML 中使用 XName 获取元素

[英]Get element using XName in XML

I have an XML file which looks like this, the call out is that namespace uses prefix, xmlns:opf .我有一个 XML 文件,看起来像这样,标注是命名空间使用前缀xmlns:opf

<?xml version="1.0" encoding="utf-8"?>
<package version="2.0" unique-identifier="uuid_id" xmlns:opf="http://www.idpf.org/2007/opf">
    <metadata xmlns:dcterms="http://purl.org/dc/terms/">
    </metadata>
</package>

Following is my code trying to get the 'package' element using the XName以下是我尝试使用 XName 获取“package”元素的代码

static void Main(string[] args)
{
    using (MemoryStream stream = new MemoryStream(File.ReadAllBytes(@"C:\Users\xyz\Downloads\XMLs\X2.xml")))
    {
        XDocument xDoc = XDocument.Load(stream);
        XNamespace opfNamespace = "http://www.idpf.org/2007/opf";

        XElement packageElement = xDoc.Element(opfNamespace + "package");

        Console.WriteLine($"packageElement is null - {packageElement == null}");
        Console.WriteLine($"Prefix - [{xDoc.Root.GetPrefixOfNamespace(opfNamespace)}] Namespace - [{xDoc.Root.GetNamespaceOfPrefix("opf")}]");

        foreach (var element in xDoc.Elements())
        {
            Console.WriteLine($"Element: Namespace - [{element.Name.NamespaceName}] Name - [{element.Name.LocalName}]");
        }
    }
}

The output of the program is -程序的output是——

packageElement is null - True
Prefix - [opf] Namespace - [http://www.idpf.org/2007/opf]
Element: Namespace - [] Name - [package]

So, it's not able to find the element using opfNamespace + "package" , for further debugging I logged the elements, and it shows that the element.Name does not contain the namespace, which explains why it's not able to find element using opfNamespace + "package" .因此,它无法使用opfNamespace + "package"找到元素,为了进一步调试,我记录了元素,它显示element.Name不包含命名空间,这解释了为什么它无法使用opfNamespace + "package"找到元素opfNamespace + "package"

Question - How do I find the 'package' element from the above XML, I don't want to iterate through all the elements manually?问题 - 如何从上面的 XML 中找到'package'元素,我不想手动遍历所有元素?

Is the XML parsing happening correctly, and the 'package' element not having namespace is expected? XML 解析是否正确发生,并且预期没有命名空间的'package'元素?

For further testing, I removed the prefix from the namespace and ran the code, this time it's able to find the 'package' element.为了进一步测试,我从命名空间中删除了前缀并运行了代码,这次它能够找到'package'元素。

XML - XML -

<?xml version="1.0" encoding="utf-8"?>
<package version="2.0" unique-identifier="uuid_id" xmlns="http://www.idpf.org/2007/opf">
    <metadata xmlns:dcterms="http://purl.org/dc/terms/">
    </metadata>
</package>

Output - Output -

packageElement is null - False
Prefix - [] Namespace - []
Element: Namespace - [http://www.idpf.org/2007/opf] Name - [package]

That package XML element is not defined in the http://www.idpf.org/2007/opf XML namespace. package XML 元素未在http://www.idpf.org/2007/opf XML 命名空间中定义。

That xmlns:opf="http://www.idpf.org/2007/opf" is just de definition of that namespace without being applied on the package XML element. xmlns:opf="http://www.idpf.org/2007/opf"只是该名称空间的定义,没有应用于package XML 元素。

This means that it is to be retrieved as below这意味着它将按如下方式检索

XElement packageElement = xDoc.Element("package");

If that package element would belong to that XML namespace, than it would be defined as如果该package元素属于该 XML 命名空间,那么它将被定义为

<opf:package version="2.0" unique-identifier="uuid_id" xmlns:opf="http://www.idpf.org/2007/opf">
    <!-- ... -->
</opf:package>

or要么

<package version="2.0" unique-identifier="uuid_id" xmlns="http://www.idpf.org/2007/opf">
    <!-- ... -->
<package>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM