2

I have xml from I want get some data

XDocument loaded = XDocument.Load(@"c:\TERC.xml"); var query = (from c in loaded.Descendants("catalog") from r in c.Descendants("row") select (string)r.Element("Name")); 

this returns me collection of null

How can I fix it ?

Here is this xml:

<?xml version="1.0" encoding="UTF-8" ?> <teryt> <catalog name="Compix"> <row> <col name="NAME">Name1</col> <col name="ID">12</col> </row> <row> <col name="NAME">Name2</col> <col name="ID">13</col> </row> <row> <col name="NAME">Name3</col> <col name="ID">14</col> </row> </catalog> </teryt> 

3 Answers 3

2
List<string> query = (from c in loaded.Descendants("catalog") from r in c.Descendants("row") from col in r.Descendants("col").Where(col1 => col1.Attribute(XName.Get("name")).Value == "NAME") select col.Value).ToList(); 

After executing statement above, query contains the following strings:

  • Name1
  • Name2
  • Name3
Sign up to request clarification or add additional context in comments.

1 Comment

sorry, I added very important value to my xml. It's only piece of this big xml, and I didn't is so important to you to know that :/
1

You may want to try this:

var query = (from c in loaded.Descendants("catalog") from r in c.Descendants("row") select (string)r.Value); 

I assume that you want to get the data inside the "col" node. Otherwise, please precise what you want to retrieve.

Hope this helps.

2 Comments

sorry, I added very important value to my xml. It's only piece of this big xml, and I didn't is so important to you to know that :/
No problem it's normal, we are not in your code so we don't know it all. I just hope some of the answers are helpful to you.
1

Name is an attribute on the col element of which you want the value, that part was missing in the query.

 var query = (from c in loaded.Descendants("catalog") from r in c.Descendants("row") from col in r.Descendants("col") select (string)r.Value).ToList<string>(); 

1 Comment

sorry, I added very important value to my xml. It's only piece of this big xml, and I didn't is so important to you to know that :/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.