Linq to XML query returining a list of strings
I have the following class
public class CountrySpecificPIIEntity
{
public string Country { get; set; }
public string CreditCardType { get; set; }
public String Language { get; set; }
public List<String> PIIData { get; set; }
}
I have the following XML that I want to query using Linq-to-xml and shape in to the class above
<?xml version="1.0" encoding="utf-8" ?>
<piisettings>
<piifilter country="DE" creditcardype="37" language="ALL" >
<filters>
<filter>FIRSTNAME</filter开发者_Python百科>
<filter>SURNAME</filter>
<filter>STREET</filter>
<filter>ADDITIONALADDRESSINFO</filter>
<filter>ZIP</filter>
<filter>CITY</filter>
<filter>STATE</filter>
<filter>EMAIL</filter>
</filters>
</piifilter>
<piifilter country="DE" creditcardype="37" Language="en" >
<filters>
<filter>EMAIL</filter>
</filters>
</piifilter>
</piisettings>
I'm using the following query but I'm having trouble with the last attribute i.e. PIIList.
var query = from pii in xmlDoc.Descendants("piifilter")
select new CountrySpecificPIIEntity
{
Country = pii.Attribut("country").Value,
CreditCardType = pii.Attribute("creditcardype").Value,
Language = pii.Attribute("Language").Value,
PIIList = (List<string>)pii.Elements("filters")
};
foreach (var entity in query)
{
Debug.Write(entity.Country);
Debug.Write(entity.CreditCardType);
Debug.Write(entity.Language);
Debug.Write(entity.PIIList);
}
How Can I get the query to return a List to be hydrated in to the PIIList property.
var query = from pii in xmlDoc.Descendants("piifilter")
select new CountrySpecificPIIEntity
{
Country = pii.Attribut("country").Value,
CreditCardType = pii.Attribute("creditcardype").Value,
Language = pii.Attribute("Language").Value,
PIIList = pii.Elements("filters").Select(xe => xe.Value).ToList();
};
i am not sure, can you check you class definition once to check if ?
public List<String> PIIList { get; set; }
if this is correct then try this:
select new CountrySpecificPIIEntity
{
Country = pii.Attribute("country").Value,
CreditCardType = pii.Attribute("creditcardype").Value,
Language = pii.Attribute("Language").Value,
PIIList = pii.Elements("filters").Cast<string>().ToList()
}
精彩评论