Linq to XML,深入挖掘属性

时间:2009-08-25 20:26:16

标签: linq-to-xml serialization attributes

我有一个看起来像这样的XML

<?xml version="1.0" encoding="utf-8" ?>
<Applicant>
  <NameValueGroup attribute="Name">
    <NameValue desc="One" value="test1"/>
    <NameValue desc="Two" value="test2"/>
    <NameValue desc="Three" value="test3"/>
    <NameValue desc="Four" value="test4"/>
  </NameValueGroup>
  <NameValueGroup attribute="News">
    <NameValue desc="news1" value="Something1"/>
    <NameValue desc="news2" value="Something2"/>
    <NameValue desc="news3" value="Something3"/>
    <NameValue desc="news4" value="Something4"/>
  </NameValueGroup>
</Applicant>

如何编写LINQ to XML查询以反序列化此XML。

2 个答案:

答案 0 :(得分:0)

这有效,但它没有空值/缺失值的错误处理。享受。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace LinqToXmlExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = XDocument.Load("input.xml");

            var content =
                from node in x.Descendants("Applicant").Descendants("NameValueGroup")
                select new NameValueGroup()
                           {
                                Attribute = node.Attribute("attribute").Value,
                                NameValues = GetNameValues(node.Descendants())
                           };

            foreach (NameValueGroup item in content)
            {
                Console.WriteLine(item.Attribute);
                foreach (var name in item.NameValues)
                {
                    Console.WriteLine("{0} - {1}", name.Desc, name.Value);
                }
            }

            Console.ReadLine();
        }

        static List<NameValue> GetNameValues(IEnumerable<XElement> elements)
        {
            var x = new List<NameValue>();
            if(elements != null && elements.Any())
            {
                x =
                    (from node in elements
                    select new NameValue()
                               {
                                   Desc = node.Attribute("desc").Value,
                                   Value = node.Attribute("value").Value
                               }).ToList();
            }
            return x;
        }

        class NameValueGroup
        {
            public string Attribute { get; set; }
            public List<NameValue> NameValues { get; set; }
        }

        class NameValue
        {
            public string Desc { get; set; }
            public string Value { get; set; }
        }
    }
}

答案 1 :(得分:0)

我创建了一个名为Applicant的类,其属性类型为Dictionary,其中键为string,另一个值为Dictonary<string, string>

public class Applicant {
    public Dictionary<string, Dictionary<string, string>> NameValueGroup { get; set; }
}

要填写此属性,我会这样做。

XDocument document = XDocument.Load("data.xml");
Applicant applicant = new Applicant();

applicant.NameValueGroup = document.Element("Applicant").
                            Descendants("NameValueGroup").
                            ToDictionary(keyElement => keyElement.Attribute("attribute").Value, valueElement => valueElement.Descendants().
                                 ToDictionary(e => e.Attribute("desc").Value, e => e.Attribute("value").Value));

请原谅糟糕的格式化。很难让这个查询很好看!