开发者

LINQ to XML query help needed

开发者 https://www.devze.com 2022-12-28 20:08 出处:网络
I have the following XML structure... <F开发者_如何学Pythonields> <Field> <Company>My Company</Company>

I have the following XML structure...

<F开发者_如何学Pythonields>
    <Field>
        <Company>My Company</Company>
    </Field>
    <Field>
        <Address2>Villa at beach</Address2>
    </Field>
    <Field>
        <Email2>email2@mail.com</Email2>
    </Field>
    <Field>
        <Mobile>333-888</Mobile>
    </Field>
    <Field>
        <ContactMethod>Facebook</ContactMethod>
    </Field>
</Fields>

I would like to know how to get the element's name using LINQ? Something like this:

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

I would like the output to be something like this:

Company: My Company
Address2: Villa at beach...


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
                          });
0

精彩评论

暂无评论...
验证码 换一张
取 消