Simon Kiely
Simon Kiely

Reputation: 6070

Get the contents of all elements with the same name using XML?

I am trying to parse some XML in my program, but I cannot seem to figure out the best way to do this.

The XML has an element called "container". This element has multiple descendants called "text". What I would like to do is pull out the values inside the "text" elements, in order, and save them in a string.

So the XML looks like :

<containers>
   <container>
      <elt3243> </elt3243>
      <elt1122></elt1122>
      <elt><text> Put me in a string please </text> </elt>
      <elt2211></elt2211>
   </container>
   <container>
      <elt3243><text>I would also like to be in the string</text></elt3243>
      <elt1122></elt1122>
      <elt> </elt>
      <elt2211></elt2211>
   </container>
</containers>

And the result I would like :

String result = "Put me in a string please \n I would also like to be in the string";

What is the best way of doing this ?

Upvotes: 1

Views: 2273

Answers (5)

Justin Harvey
Justin Harvey

Reputation: 14682

You could use XPath, see here for more on MSDN.

So, something like this:

XPathDocument document = new XPathDocument(<Source of your XML >);
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("//text");

string result = "";
while (nodes.MoveNext())
{
    result += nodes.Current.Value + "\n";
}

Upvotes: 0

Tieson T.
Tieson T.

Reputation: 21246

Here's a Win32 console example that should demonstrate what you could do:

var doc = XDocument.Parse(
    "<containers>" + 
        "<container>"+
          "<elt3243> </elt3243>"+
          "<elt1122></elt1122>"+
          "<elt><text> Put me in a string please </text> </elt>"+
          "<elt2211></elt2211>"+
       "</container>"+
       "<container>"+
          "<elt3243><text>I would also like to be in the string</text></elt3243>"+
          "<elt1122></elt1122>"+
          "<elt> </elt>"+
          "<elt2211></elt2211>"+
       "</container>"+
    "</containers>", LoadOptions.None);

var text = doc.Descendants("text").Select(x => x.Value);
Console.WriteLine(string.Join(" \n ", text));
Console.ReadKey(false);

Note that you don't really need to project this into an array or list, as some earlier answers indicate. string.Join just wants a separator and an array of objects; IEnumerable<string> to an array is handled implicitly by the runtime. To test it, create a new C# Console app, and drop this into Main(). Should work as-is...

Upvotes: 1

Bobson
Bobson

Reputation: 13716

XDocument doc = XDocument.Load(file.FullName);
var strings = doc.Descendants("container").SelectMany(x => x.Descendants("text")).ToList();
return strings.Join(" \n ");

Upvotes: 2

Daniel Persson
Daniel Persson

Reputation: 2233

XDocument is your friend...

var doc = XDocument.Load(...filename...);
var strings = doc.Descendants("text").Select(x => x.Value).ToArray();
var result = string.Join("\r\n", strings);

Maybe there even is a simpler way using the ForEach extension method...

Upvotes: 0

atredis
atredis

Reputation: 382

Try this

XDocument xDocument = new XDocument();
//load xml
StringBuilder sb = new StringBuilder();

foreach(var text in xDocument.Descendants("text") )
{
sb.append(text.value)
}

Upvotes: 1

Related Questions