将类从 Linq 填充为 XML
好的,我有一个几乎如下所示的 XML 文件:
<Templates>
<Template>
<TemplateID>00X500000011iTSEAY</TemplateID>
<To>
<To type='User'>00550000000mfOU</To>
<To type='User'>00550000000mb3pAAA</To>
<To type='Group'>00G50000001PiHz</To>
<To type='AccountTeam'>AccountExecutive</To>
</To>
<CC>
<CC type='AccountTeam'>SalesLead</CC>
</CC>
<BCC>
<BCC type='SalesTeam'>SalesManager</BCC>
</BCC>
</Template>
</Templates>
有了这个文件,我要做的就是填充一些可以理解它的类。所以我对 To Elements 的第一个想法是这样的:
public class EmailRecipient
{
public ToRecipient To {get;set;}
public CCRecipient CC {get;set;}
}
public class ToRecipient
{
public string Key {get;set;}
public string Type {get;set;}
}
不完全优雅,但这是我的草稿。我尝试使用 Linq to XML 来实现此目的,因为我知道我拥有什么 TemplateId,然后我可以用这些值填充该类。这就是我所处的位置……并陷入了这个测试应用程序中:
var results = from x in xmlDoc.Descendants(XName.Get("Template")
where x.Element(XName.Get("TemplateID")).Value == "00X500000011iTSEAY"
select new EmailRecipient()
{
ToRecipient =
};
话虽如此,我希望获得“Tos”的集合,其中每个“to”都可以是“type”值。
任何帮助将不胜感激,过去 3 天我在 XML 方面所做的工作比过去 3 年还要多,但这并不多。
非常感谢您的帮助。
Ok, so I have an XML file that pretty much looks like this:
<Templates>
<Template>
<TemplateID>00X500000011iTSEAY</TemplateID>
<To>
<To type='User'>00550000000mfOU</To>
<To type='User'>00550000000mb3pAAA</To>
<To type='Group'>00G50000001PiHz</To>
<To type='AccountTeam'>AccountExecutive</To>
</To>
<CC>
<CC type='AccountTeam'>SalesLead</CC>
</CC>
<BCC>
<BCC type='SalesTeam'>SalesManager</BCC>
</BCC>
</Template>
</Templates>
With that, what I'm trying to do is to populate some classes that can make sense of that. So the first thought I had was something like this for the To Elements:
public class EmailRecipient
{
public ToRecipient To {get;set;}
public CCRecipient CC {get;set;}
}
public class ToRecipient
{
public string Key {get;set;}
public string Type {get;set;}
}
Not entirely elegant, but this is my rough draft. I'm trying to use Linq to XML for this since I know what TemplateId I have, and then I can just populate the class with the values. Here's where I'm at...and stuck in this test app:
var results = from x in xmlDoc.Descendants(XName.Get("Template")
where x.Element(XName.Get("TemplateID")).Value == "00X500000011iTSEAY"
select new EmailRecipient()
{
ToRecipient =
};
With all of that being said, I'm looking to get a collection of "Tos" where each "to" can a "type" value.
Any help would be greatly appreciated, I've done more in with XML in the past 3 days than have in the past 3 years, and that ain't much.
Thanks so much for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于每个 EmailRecipient 实例有多个 ToRecipient,因此 To 属性的类型需要是集合类型。如果将其设为 IEnumerable:
那么这个 LINQ 查询应该可以解决问题:
Since you have more than one ToRecipient per EmailRecipient instance, the type of the To property will need to be a collection type. If you make it an IEnumerable:
Then this LINQ query should do the trick: