在XElement获取子节点值

时间:2013-06-21 11:05:28

标签: c# xml xml-parsing

我尝试使用C#获取子节点的XElement值。 XElement块如下。我的目的是在'c0'节点获取值。我怎么能这样做?

// client is Orion SDK (SWIS) client.
XElement xe = client.QueryXml("select Uri FROM Orion.Pollers WHERE NetObjectID = 15", null);

XElement结果块:

<queryResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.solarwinds.com/2007/08/informationservice">
<template>
<resultset>
  <column name="Uri" type="String" ordinal="0" /> 
</resultset>
</template>
<data>
    <row>
        <c0>swis://solarwinds./Orion/Orion.Pollers/PollerID=55</c0> 
    </row>
    <row>
        <c0>swis://solarwinds./Orion/Orion.Pollers/PollerID=56</c0> 
    </row>
    <row>
        <c0>swis://solarwinds./Orion/Orion.Pollers/PollerID=57</c0> 
    </row>
    <row>
        <c0>swis://solarwinds./Orion/Orion.Pollers/PollerID=58</c0> 
    </row>
  </data>
</queryResult>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。代码块如下。

    XElement xe = client.QueryXml("SELECT Uri FROM Orion.Pollers WHERE NetObjectID = 15", null);

    IList<XElement> indexedElements = xe.Elements().ToList();

    foreach (var item in ((XElement)xe.Elements().ToList()[1]).Elements().ToList())
    {
        try
        {
            //Do something with 
            //item.Value
        }
        catch (Exception exc)
        {
            throw exc;
        }


    }