2011-03-18 68 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();