0

XML,

 <A>
    <B  id="ABC">
      <C name="A" />
      <C name="B" />
      <C name="C" />
      <C name="G" />
    </B>
    <B id="ZYZ">
      <C name="1" />
      <C name="2" />
      <C name="3" />
      <C name="4" />
    </B>
  </A>

I Need to select only <C> nodes and assign to a variable.

I'm using following c# code which is not working,

var asdf = c.Elements("C");

asdf should contain ,

<C name="A" />
<C name="B" />
<C name="C" />
<C name="G" />
<C name="1" />
<C name="2" />
<C name="3" />
<C name="4" />

Any help appreciated!

1
  • 1
    What is c here? You haven't given us enough context. It sounds like you're just looking for the Descendants("C") method instead of Elements("C"). Commented Mar 13, 2014 at 12:57

2 Answers 2

2

You need Descendants like:

var cNodes = document.Descendants("C");

For output:

foreach (var item in cNodes)
{
    Console.WriteLine(item);
}

You will get:

<C name="A" />
<C name="B" />
<C name="C" />
<C name="G" />
<C name="1" />
<C name="2" />
<C name="3" />
<C name="4" />
Sign up to request clarification or add additional context in comments.

Comments

2

this run:

XElement x = XElement.Parse("your_xml_string");

var asdf=x.Elements("B").Elements("C");

1 Comment

TLama what is changed with your edit? Excuse me but I new here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.