开发者

How retrieve deeper siblings using LINQ to XML?

I have an XML structure as follows. I need to extract "Value" and "String" by matching the comm开发者_如何学Goand attributes? How to write LINQ for this?

 <Root>
    <Command val="1001" type="sync">
      <Status>
        <DataList>
          <Info>
            <Value>1</Value>
            <String>Sample String 1 is set</String>
          </Info>
          <Info>
            <Value>2</Value>
            <String>Sample String 2 is set</String>
          </Info>
          <Info>
            <Value>3</Value>
            <String>Sample String 3 is set</String>
          </Info>
        </DataList>
      </Status>
    <Command>
</Root>

I tried something as below but exception occurred while running.

lst = (
    from command in xmlDoc.Descendants("Command")
        .Descendants("Status")
        .Descendants("DataList")
    select new EnumList
    {
        val = command.Element("Value").Value,
        stringVal = command.Element("String").Value,
    })
    .ToList();


Try

lst = (
    from command in xmlDoc.Descendants("Info")
    select new EnumList
    {
        val = command.Element("Value").Value,
        stringVal = command.Element("String").Value,
    })
    .ToList();

and you have error in xml sample (no close tag Command), change it to

    </Command>
</Root>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜