Rod
Rod

Reputation: 15475

text file of random xml nodes

i have a snippet of xml that i've pasted in a text file which i would like to parse the value of the name attribute in a console app.

    string myXmlString =   

        @"<?xml version=""1.0""?>
        <!-- comment at the root level -->
        <Root>
            <data name=""FIELD_ONE"" xml:space=""preserve""> 
                <value>Accompaniment:</value> 
            </data> 
            <data name=""FIELDS_TWO"" xml:space=""preserve""> 
                <value>Accompaniment Detail</value> 
            </data> 
        </Root>";

i'd like it to just display the value in my console so i can copy and paste it

Should look like the following:

FIELD_ONE
FIELD_TWO

Upvotes: 1

Views: 177

Answers (1)

msarchet
msarchet

Reputation: 15242

You should use XDocument

Create the XDocument with either XDocument.Load or XDocument.Parse depending if you are loading from a file or a string.

Then to get each of the values you could write.

XDocument yourXML = //load it here;

foreach(var element in yourXML.Elements("Root").Elements("data")) 
{
    Console.WriteLine(element.Attribute("name").ToString());
}

Upvotes: 1

Related Questions