views:

218

answers:

2

Hi,

I'm submitting a command to a ssh session and getting an XML response back which is variable depending on the type of query I'm running. I get the following type of XML returned...

<CLIOutput>
  <Results>
    <ReturnCode>0</ReturnCode>
    <EventCode>23000</EventCode>
   <EventSummary>CLI command completed successfully.</EventSummary>
  </Results>
  <Data>
    <Row>
      <Client>kcllaptop</Client>
      <Domain>/Top/Top</Domain>
    </Row>
    <Row>
      <Client>testclient</Client>
      <Domain>/Top/Top</Domain>
    </Row>
  </Data>
</CLIOutput>

I then parse into an XDocument, and what I want to do is Enumerate through the different <Row> attributes in the <Data> section, given they change. They're always in DATA section, but the attribute names and numbers of them change. I can get the specific one in the example above, but I'm after a more generic method.

I can get to the specifcs by

_xDoc.Elements().<Data>.<Rows>(0).<Client>.ToValue

but the <Client> name changes.

What's the best way to enumerate through the rows returned in the element.

Complete LInq newbie sorry.

Thanks and Cheers, Al

A: 

I assume you're using VB.Net.

You can loop over the return value of the Elements method, like this:

For Each client As XElement In _xDoc.Elements().<Data>.<Row>
    'Do something
Next
SLaks
Should be .<Row> at the end instead of .<Rows>
Dennis Palmer
A: 

The answer by SLaks works. However, in VB.NET you don't need the .Elements() method.

This will do the same thing:

For Each row in _xDoc.<Data>.<Row>
    Console.WriteLine(row.<Client>.Value)
Next
Dennis Palmer
Sorry - what I meant was, it might not be <Client> next time, it could be <Domain>. I ended up getting what I wanted to work in C#, and then converted it to VB.Net - not a VB.Net developer, but having to integrate something into an existing library.
Al Ashcroft
OK, I can help with VB.NET syntax if needed.
Dennis Palmer