Linq to XML 查询返回字符串列表
我有以下类
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定,你能检查一下你的类定义吗?
如果这是正确的,那么试试这个:
i am not sure, can you check you class definition once to check if ?
if this is correct then try this: