sanar
sanar

Reputation: 447

XML to list using LINQ

have an XML file like this:

<VS>
  <Fields>
    <Field Id="$1*">Column1</Field>
    <Field Id="$2*">Column2</Field>
    <Field Id="$3*">Column3</Field>
  </Fields>
</VS>

When I use LINQ to XML using the following statement:

 XDocument doc = XDocument.Parse(s);
var q = doc.Descendants("Fields").ToList();

I get a single entry in list as Column1Column2Column3, whereas I want it as 3 separate entities like Column1, Column2, Column3.

Can anyone help?

Upvotes: 2

Views: 6143

Answers (3)

sanar
sanar

Reputation: 447

Just used the following code which returned list of strings.

var q = doc.Descendants("Field").Select(x => x.Value);

Thanks for all your suggestions!

Upvotes: 0

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Use Field instead of Fields:

var q = doc.Descendants("Field").ToList();

Upvotes: 4

Viral Shah
Viral Shah

Reputation: 2246

you should use XElement.Parse(BuildNode.InnerXml) instead of just passing the raw property in.

Check This

Upvotes: 0

Related Questions