Given the following XML section from a large xml file:
<item id="C3DD6846593" >
<name xml:lang="fr">Atlantique</name>
<name xml:lang="en">Atlantic </name>
</item>
I’m trying to load an object in Linq based on the attribute of a node. Here is what I tried to do in the code.
public class MyList
{
public string ID {get;set;}
public string EnName {get;set;}
public string FrName {get;set;}
public MyList() { }
}
…… later in code
AgencyList = (from x in rawSrc.Descendants("item")
select new MyList{
ID = x.Attribute("id").Value
EnName = x.XPathSelectElement("\\name[@lang='en']").Value,
FrName =x.XPathSelectElement("\\name[@lang='fr']").Value
}).ToList();
I get ” ‘\name[@lang=’en’]’ has an invalid token.” as the error. Is there another way to approach this?
You need to use a normal slash instead of a backslash. Additionally, you need to add the namespace of the attribute and pass in an instance of
IXmlNamespaceResolver: