views:

24

answers:

1
<Fields>
    <Field>
        <Company>My Company</Company>
    </Field>

    <Field>
        <Address2>Villa at beach</Address2>
    </Field>

    <Field>
        <Email2>[email protected]</Email2>
    </Field>

    <Field>
        <Mobile>333-888</Mobile>
    </Field>

    <Field>
        <ContactMethod>Facebook</ContactMethod>
    </Field>

</Fields>

How to get element's name using LINQ?

var fields = (from field in contact.XmlFields.Descendants("Field")
                          select new ContactXmlView
                          {
                              Field = ...,
                              Value = ...
                          });

So that I can have such output: Company: My Company
Address2: Villa at beach...

Thanks in advance,
Ile

+1  A: 

Assuming that you have only one descendant for each "Field" node :

var fields = (from field in contact.XmlFields.Descendants("Field")
                          select new ContactXmlView
                          {
                              Field = field.Descendants().First().Name,
                              Value = field.Descendants().First().Value
                          });
Olivier PAYEN
Olivier, thank you!
ile