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
});
精彩评论