Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

could anyone help me?
i have an xml file

XML
<?xml version="1.0" encoding="utf-8"?>
<Description Version="1">
  <MetaData>
    <aa>4008321964403</aa>
    <bb>1</bb>
    <cc>0</cc>
    <dd>A63238E</dd>
    <ee>select</ee>
    <ff>DALI</ff>
    <gg>true</gg>
  </MetaData>
</Description >


i want to read all child element of metadata using linq.

i have checked with
C#
var loadecg = (from elem in xElement.Elements("MetaData")
                           select elem);



but it is coming in string form i want one by one using 1 query because i also want to display these child element values.

Thanks
Posted
Updated 10-Dec-13 22:41pm
v3
Comments
Sergey Alexandrovich Kryukov 10-Dec-13 12:02pm    
This is not XML! Just a typo? Use Copy/Paste. :-)
—SA

1 solution

First of all, I don't know whether the end tags are wrong at your side or it is just a posting issue here.
XML
<bb>1</bbr>
<cc>0</ccr>

It should be </bb> and </cc>.
XML
<bb>1</bb>
<cc>0</cc>


Solution

You need use xElement.Elements("MetaData").Descendants().
C#
var loadecg = from elem in xElement.Elements("MetaData").Descendants()
              select elem;

string value = string.Empty;

foreach (var descendant in loadecg)
{
    // Get the value for each Descendant.
    value = descendant.Value;
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Dec-13 12:02pm    
Due to this two lines you mentioned in first code sample, the correct answer should be:

This is not XML. You cannot parse it as XML. Make it well-formed first.

—SA
Oh yes :D you are correct Sergey. Still I provided the code to read the XML if OP makes it well formatted after seeing the answer. :)

Thanks,
Tadit
bunty swapnil 11-Dec-13 4:48am    
yes tadit but i tried above code that is also not working.
Did you correct your XML?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900