2

我有以下xml:

<?xml version="1.0" encoding="UTF-8"?>
<video contenttype="asf" fileextension=".wmv" hascontent="no" lang="en-GB" length="430" pid="45202118763477" sid="">
    <lastmodified timestamp="1366113600000">
        Tue, 16 Apr 2013 12:00:00 +0000
    </lastmodified>
    <links/>
    <keywords/>
    <slides/>
    <copyright>
        Copyright owned by original broadcaster
    </copyright>
    <title>
        BBC News
    </title>
    <comment>
        The latest news updates from around the world.
    </comment>
    <author>
        BBC World Sv_
    </author>
    <email/>
    <captioning/>
    <extendeddata>
        <data name="keepOriginal">
            0
        </data>
        <data name="keepTranscoded">
            0
        </data>
        <data name="realStartTime">
            1366113604
        </data>
        <data name="scheduledStartTime">
            1366113600
        </data>
        <data name="broadcastLength">
            360
        </data>
        <data name="broadcastChannel">
            BBC World Sv_
        </data>
        <data name="paddingUsed">
            0
        </data>
        <data name="transcodingSpec"/>
        <data name="transcoding">
            succeeded
        </data>
        <data name="transcodingProfile">
            -b 2.35M -a 128k --debug  --primary-format mp4 --podcast &quot;-l 270 -b 400 -R 48 -B 64&quot; --keep-files true
        </data>
        <data name="transcoderDetails">
            ver 1.58.2.5, 2013-04-16  13:08:08 up 42 days, 12:00,  0 users,  load average: 0.35, 0.13, 0.07, OS20031 212.70.71.204
        </data>
        <data name="originalFilename">
            /var/lib/etvd/mpegts/BBC World Sv_/BBC News (16-Apr-2013 13.00).emcast
        </data>
        <data name="originalRecordingTime">
            created 2013-04-16 , last modified 2013-04-16 : recording lasted 0s
        </data>
        <data name="primaryFormat">
            mp4
        </data>
        <data name="doXml">
            True
        </data>
        <data name="doFiles">
            True
        </data>
    </extendeddata>
    <categories>
        <category name="em:podcast">
            mp4
        </category>
    </categories>
</video>

使用 XDocument,我可以进入<extendeddata>返回所有数据节点和值的精细的后代,但是我如何根据数据属性的名称专门获取一个值?因此,例如,我想进入一个特定的元素,例如:<data-name="realStartTime">1366113604</data>1366113604 作为我想退出的值。

干杯

4

2 回答 2

1

您需要基本上过滤您想要的一个元素。一旦你调用Descendants了 ,你就有了一个元素的集合,你需要过滤那些具有name值为 的属性的元素realStartTime。这是实现这一目标的一种方法:

var realStartTime =
    xdoc.Descendants("data")
        .Single(x => x.Attribute("name").Value == "realStartTime")
        .Value
        .Trim();

的值realStartTime将是1366113604

于 2013-04-17T17:12:21.257 回答
0

获取extendeddata元素后,您需要像这样过滤属性data上的子元素name

var doc = XDocument.Parse(...);

var result = doc.Descendants("extendeddata")
    .Elements()
    .Single(x => x.Attribute("name") != null && x.Attribute("name").Value == "realStartTime")
    .Value;
于 2013-04-17T17:12:26.860 回答