Reputation: 2013
I have all these XML records that are kind of written in two styles, they both contain the same information.
I'm new to LinQ but I thought this expression would just fine anything with a name
attribute. But it fails and returns true.
/* the failed query for the first Xml document */
/* I believed this said, find anything with a name attribute and return it's value */
public XDocument MyDocument= new XDocument().Load("this.xml");
List<string> allNames = MyDocument.Descendants()
.Where( a => a.Attribute("name") != null )
.Select( a => a.Value ).toList();
Here's two version's of xml
<RecListImgx>
<locale id="1033" />
<IMGPAK>
<img name="1033sm.jpg"/>
<img name="1033m.jpg"/>
<img name="1033r.jpg"/>
<img name="1033l.jpg"/>
</IMGPAK>
</RecListImgx>
<RecListImgx>
<locale id="1033" />
<IMGPAK>
<img>
<name> 1033sm.jpg </name>
</img>
<img>
<name> 1033s.jpg </name>
</img>
<img>
<name> 1033m.jpg </name>
</img>
<img>
<name> 1033l.jpg </name>
</img>
</IMGPAK>
</RecListImgx>
1. Is there a single query that can get the name
field from both of these styles of XML?
2. What is the proper syntax for parsing the first type?
I managed to navigate the second form just fine. But inline-attributes seem to fail to find the name attribute.
3. What's the difference in these formats?
Upvotes: 0
Views: 54
Reputation: 1502396
Is there a single query that can get the name field from both of these styles of XML?
Yes.
List<string> allNames = MyDocument.Descendants("img")
// Look for a name attribute first; if there isn't one, find the name
// element instead.
.Select(a => (string) a.Attribute("name") ?? a.Element("name").Value)
.ToList();
What is the proper syntax for parsing the first type? I managed to navigate the second form just fine. But inline-attributes seem to fail to find the name attribute.
You're finding all the elements that have a name attribute - but then selecting the value of the element, not the value of the attribute. See above.
What's the difference in these formats?
Well one has a name
child element with the value as a text node, whereas the other has a name
attribute with the value as the attribute value.
Upvotes: 2