开发者

Instantiate object from Linq to XML Query

I have the following class

public class CountrySpecificPIIEntity
{
 public string Country { get; set; }

 public string CreditCardType { get; set; }

 public String Api { get; set; }

 public List<String> FilterList { get; set; }
}

I am trying to use a linq to XMl query to create a list of instances of type CountrySpecificPIIEntity. The XML is as below

<piisettings>
 <country countrycode="DE">
  <creditcardype credicardtype="mastercard">
   <api api="开发者_StackOverflow中文版INSERT_FILTERS">
    <filters>     
     <filter>FIRSTNAME</filter>
     <filter>HOUSENUMBER</filter>
     <filter>ADDITIONALADDRESSINFO</filter>
     <filter>EMAIL</filter>
    </filters>
   </api>
  </creditcardype>
  <creditcardype credicardtype="visa">
   <api api="INSERT_FILTERS">
    <filters>
     <filter>STATE</filter>
     <filter>EMAIL</filter>
    </filters>
   </api>
  </creditcardype>
  <creditcardype credicardtype="visa">
   <api api="UPDATE_FILTERS">
    <filters>
     <filter>STATE</filter>
     <filter>EMAIL</filter>
    </filters>
   </api>
  </creditcardype>      
 </country>
 <country countrycode="GB">
  <creditcardype credicardtype="americanexpress">
   <api api="INSERT_FILTERS">
    <filters>
     <filter>STREET</filter>
     <filter>ZIP</filter>
     <filter>CITY</filter>
     <filter>STATE</filter>
     <filter>EMAIL</filter>
    </filters>
   </api>
  </creditcardype>
  <creditcardype credicardtype="debank">
   <api api="INSERT_FILTERS">
    <filters>
     <filter>IPADDRESSCUSTOMER</filter>
     <filter>FIRSTNAME</filter>
     <filter>SURNAME</filter>
     <filter>HOUSENUMBER</filter>
     <filter>STREET</filter>
     <filter>ZIP</filter>
     <filter>CITY</filter>
     <filter>STATE</filter>
     <filter>EMAIL</filter>
    </filters>
   </api>
  </creditcardype>
 </country> 
</piisettings>

what should the Linq to XML query be?


How about:

    var qry = from country in XElement.Parse(xml).Elements("country")
              from cc in country.Elements("creditcardype")
              let api = cc.Element("api")
              select new CountrySpecificPIIEntity
              {
                  Country = (string)country.Attribute("countrycode"),
                  CreditCardType = (string)cc.Attribute("credicardtype"),
                  Api = (string)api.Attribute("api"),
                  FilterList = new List<string>(
                      from filter in api.Element("filters").Elements("filter")
                      select filter.Value)
              };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜