2011-03-18 113 views
1

我有一个看起来像这样的XElement;XElement返回节点值

<VideoFiles> 
    <VideoFileInfo> 
    <VideoType>1000</VideoType> 
    <FormatCode>1000</FormatCode> 
    <Url>http://www.idontwantthisvalue.com</Url> 
    </VideoFileInfo> 
    <VideoFileInfo> 
    <VideoType>WMVOriginal</VideoType> 
    <FormatCode>1004</FormatCode> 
    <Url>http://www.iwanthitsvalue.com</Url> 
    </VideoFile> 

我需要抓住的是与1004

值同级任何人都可以在此帮助的价值?

回答

1

通常:

/VideoFiles/VideoFileInfo[FormatCode='1004']/Url 

究竟

我要抓住具有 兄弟与一个正确答案一1004

/VideoFiles/VideoFileInfo/*[.='1004']/following-sibling::*[1] 

//*[.='1004']/following-sibling::*[1] 
+0

+1值的值。 – Flack 2011-03-19 09:17:10

0

下面的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <xsl:value-of select="VideoFiles/VideoFileInfo[FormatCode='1004']/Url"/> 
    </xsl:template> 
</xsl:stylesheet> 

只输出所需的值:

http://www.iwanthitsvalue.com 
1

纯LINQ到XML溶液:

XElement xdoc = XElement.Load("test.xml"); 
    var myUrl = xdoc.Descendants("VideoFileInfo") 
        .Where(x => x.Element("FormatCode").Value == "1004") 
        .Select(x => x.Element("Url").Value) 
        .FirstOrDefault();