我有以下XML结构......
<Fields>
<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>
我想知道如何使用LINQ获取元素的名称?像这样:
var fields = (from field in contact.XmlFields.Descendants("Field")
select new ContactXmlView {Field = ...,Value = ...});
我希望输出是这样的:
Company: My Company
Address2: Villa at beach...
答案 0 :(得分:1)
假设每个“Field”节点只有一个后代:
var fields = (from field in contact.XmlFields.Descendants("Field")
select new ContactXmlView
{
Field = field.Descendants().First().Name,
Value = field.Descendants().First().Value
});