LINQ to XML get value
This is a newbie question, but I can't seem to find the following:
XML is
<sets><set><title>hello1</title><images><image>1667</image></images></set></sets>
foreach (XElement setNode in collectionXML.DescendantNodes())
{
myString = setNode.Descendants("title").First()....
}
From First (), how do I get the internal value of the header of a node? (in this case it will be "hello1")
Calling ToString () on the element gives "hello1", which is clearly not exactly what I want
+2
source to share
2 answers
I know this has already been accepted and accepted, but I can't help but point out that you can do more with LINQ.
collectionXML
.Elements("sets")
.Elements("set")
.Select(c => c)
.Each(x => SetValue(x));
void SetValue(XElement element)
{
myString = element.GetElementValue("title");
}
// Each extension method (needs to be in a static class)
public static void Each<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (var item in items) action(item);
}
0
source to share