将类从 Linq 填充为 XML

发布于 2024-09-26 19:58:00 字数 1554 浏览 2 评论 0原文

好的,我有一个几乎如下所示的 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 技术交流群。

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

发布评论

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

评论(1

划一舟意中人 2024-10-03 19:58:00

由于每个 EmailRecipient 实例有多个 ToRecipient,因此 To 属性的类型需要是集合类型。如果将其设为 IEnumerable:

    public class EmailRecipient
    {
        public IEnumerable<ToRecipient> To { get; set; }

    }

那么这个 LINQ 查询应该可以解决问题:

var results = from x in xmlDoc.Descendants(XName.Get("Template"))
              where x.Element(XName.Get("TemplateID")).Value == "00X500000011iTSEAY"
              select new EmailRecipient()
              {
                  To = (from to in x.Element("To").Elements("To")
                        select new ToRecipient { Key = to.Value, Type = to.Attribute("type").Value}).ToList()
              };

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:

    public class EmailRecipient
    {
        public IEnumerable<ToRecipient> To { get; set; }

    }

Then this LINQ query should do the trick:

var results = from x in xmlDoc.Descendants(XName.Get("Template"))
              where x.Element(XName.Get("TemplateID")).Value == "00X500000011iTSEAY"
              select new EmailRecipient()
              {
                  To = (from to in x.Element("To").Elements("To")
                        select new ToRecipient { Key = to.Value, Type = to.Attribute("type").Value}).ToList()
              };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文