views:

49

answers:

3

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>
+1  A: 

Hi.

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.

Hal
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.
Hal
+2  A: 
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
Donut
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 :/
Updated my answer to work with your updated XML.
Donut
+1  A: 

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>();
BrokenGlass
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 :/