在XElement处获取子节点值

本文关键字:子节点 获取 XElement | 更新日期: 2023-09-27 18:19:34

我尝试使用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>

在XElement处获取子节点值

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

    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;
        }

    }