Linq to XML 查询返回字符串列表

发布于 2024-08-25 05:23:13 字数 2070 浏览 6 评论 0原文

我有以下类

public class CountrySpecificPIIEntity
{
    public string Country { get; set; }
    public string CreditCardType { get; set; }
    public String Language { get; set; }
    public List<String> PIIData { get; set; }        
}

我有以下 XML,我想使用 Linq-to-xml 进行查询并将其整形为上面的类

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

我正在使用以下查询,但我在最后一个属性(即 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);
            }

如何获取查询以返回要合并到 PIIList 属性中的列表。

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.

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

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

发布评论

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

评论(2

顾冷 2024-09-01 05:23:13
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();
};
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();
};
琴流音 2024-09-01 05:23:13

我不确定,你能检查一下你的类定义吗?

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

如果这是正确的,那么试试这个:

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

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