XElement返回节点值

时间:2011-03-18 23:54:30

标签: xml linq xpath

我有一个看起来像这样的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的兄弟的值。

有人可以为此提供帮助吗?

3 个答案:

答案 0 :(得分:1)

纯LINQ to 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();

答案 1 :(得分:1)

典型地:

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

完全

  

我需要获取具有的值   兄弟姐妹,价值1004。

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

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

答案 2 :(得分: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