将 Linq 对象实例化为 XML 查询

发布于 2024-08-26 07:31:12 字数 2297 浏览 7 评论 0原文

我有以下类,

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

 public string CreditCardType { get; set; }

 public String Api { get; set; }

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

我尝试使用 linq to XMl 查询来创建 CountrySpecificPIIEntity 类型的实例列表。 XML 如下:

<piisettings>
 <country countrycode="DE">
  <creditcardype credicardtype="mastercard">
   <api api="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>

Linq to XML 查询应该是什么?

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="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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2024-09-02 07:31:12

怎么样:

    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)
              };

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)
              };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文