views:

43

answers:

1

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.

+1  A: 

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()
              };
driis
Thanks, it was the sub query (or possibility of one) that was getting me. Cheers!
BryanGrimes