views:

113

answers:

2

Hi all,

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>
                  <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.

+3  A: 
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();
};
Lee
`xe => xe.Value` would be better I guess?
Jan Jongboom
@Jan Jongboom - Yes it probably is better, I thought Value returned object. Thanks.
Lee
Hi Lee,Thank you for your reply, The code returns a list with one item in it and the value of that item is FIRSTNAMESURNAMESTREETADDITIONALADDRESSINFOZIPCITYSTATEEMAIL for the first piifilter node.
Hi LeeI found the following works correctly and returns a List<string> PIIList = pii.Elements("filters").Descendants().Select(xe => xe.Value).ToList()Thank you for pointing me in the right direction!
A: 

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()
}
Asad Butt
Hi AsadThank you for your reply The code above gives me the following error "Unable to cast object of type 'System.Xml.Linq.XElement' to type 'System.String'."