简体   繁体   English

如何使用两个命名空间为XML编写xpath?

[英]How to write xpath for xml with two namespaces?

I am trying to get the values of the title and link with the attribute equals to alternate. 我试图获取标题的值,并与等于替代属性的链接。 But with the namespaces, I find it a bit challenging to get the values. 但是对于命名空间,我发现获取值有些挑战。

I have added my namespaces as follows but my result is comming back with Enumeration yeilds no result: 我添加了我的命名空间,如下所示,但结果返回Enumeration yelds无结果:

nameSpaceManager_ = new XmlNamespaceManager(new NameTable());
nameSpaceManager_.AddNamespace("viz", "http://www.vizrt.com/atom");
nameSpaceManager_.AddNamespace("atom", "http://www.w3.org/2005/Atom");

I am using XDocument with a mixture of linq and xpath to query my data. 我将linq和xpath混合使用XDocument来查询数据。

I use the XPath as follows: 我使用XPath如下:

var showName = showNode.XPathEvaluate("/atom:entry/atom:title/text()", nameSpaceManager_);

UPDATE 更新

XML: XML:

<?xml version="1.0" encoding="UTF-8"?>
<feed xml:base="http://127.0.0.1:8580/directory/shows/" xmlns="http://www.w3.org/2005/Atom">
    <entry xmlns="http://www.w3.org/2005/Atom" 
    xmlns:viz="http://www.vizrt.com/atom">
        <title>My Show</title>
        <author>
            <name>Media Sequencer</name>
        </author>
        <id>tag:user,2017-02-03:0:/directory/shows/My%20Show.show</id>
        <updated>2017-02-03T11:41:05Z</updated>
        <summary>Show My Show</summary>
        <category scheme="http://www.vizrt.com/types" term="directory" />
        <category scheme="http://www.vizrt.com/types" term="show" />
        <category scheme="http://www.vizrt.com/types" term="trio_4_layer_collection" label="Trio 4 Layer Collection" />
        <link type="application/atom+xml;type=feed" rel="alternate" href="http://127.0.0.1:8580/show/%7B4575C71F-FC79-4813-A92F-D6297D5C517C%7D/" />
        <link type="application/atom+xml;type=entry" rel="self" href="http://127.0.0.1:8580/directory/shows/My%20Show.show" />
        <viz:empty>false</viz:empty>
    </entry>
    <entry xmlns="http://www.w3.org/2005/Atom" 
    xmlns:viz="http://www.vizrt.com/atom">
        <title>My Show 2</title>
        <author>
            <name>Media Sequencer</name>
        </author>
        <id>tag:user,2017-02-03:0:/directory/shows/My%20Show.show</id>
        <updated>2017-02-03T11:41:05Z</updated>
        <summary>Show My Show</summary>
        <category scheme="http://www.vizrt.com/types" term="directory" />
        <category scheme="http://www.vizrt.com/types" term="show" />
        <category scheme="http://www.vizrt.com/types" term="trio_4_layer_collection" label="Trio 4 Layer Collection" />
        <link type="application/atom+xml;type=feed" rel="alternate" href="http://127.0.0.1:8580/show/%7B4575C71F-FC79-4813-A92F-D6297D5C517C%7D/" />
        <link type="application/atom+xml;type=entry" rel="self" href="http://127.0.0.1:8580/directory/shows/My%20Show.show" />
        <viz:empty>false</viz:empty>
    </entry>
</feed>

Updated Query : 更新的查询

var exEl = xmlDoc.XPathSelectElements("//atom:feed/atom:entry[atom:category/@term='show']", nameSpaceManager_);

foreach (var showNode in exEl.Cast<XElement>())
{
    var showName = showNode.XPathSelectElement("/atom:entry/atom:title", nameSpaceManager_).Value;

    var linkTypes = showNode.XPathSelectElements("/atom:entry/atom:link[@rel='alternate']", nameSpaceManager_)
            .Select(e => e.Attribute("type").Value);
}

You do almost everything right. 您几乎做对了所有事情。

Just use XPathSelectElement instead of XPathEvaluate . 只需使用XPathSelectElement而不是XPathEvaluate

var showName = showNode.XPathSelectElement("/atom:entry/atom:title", nameSpaceManager_).Value;

var linkTypes = showNode.XPathSelectElements("/atom:entry/atom:link[@rel='alternate']", nameSpaceManager_)
    .Select(e => e.Attribute("type").Value);

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

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