【问题标题】:how do I parse xml into a tree of objects using linq only?如何仅使用 linq 将 xml 解析为对象树?
【发布时间】:2012-06-07 11:54:49
【问题描述】:

我正在尝试将 xml 解析为对象的层次结构,但我不确定如何正确地递归层次结构。我确实有一个粗略的解决方案(不是一个好的解决方案),即调用 GetChild 来解析 XElement 并返回一个集合。我希望有人知道如何在纯 linq 表达式中实现这一点,即。将 Parent-Child-Item 关系填充到 List 中,而无需内联调用 GetChild() 等函数

谢谢

var element = XElement.Parse(@"<Root RegisterVersion='1.0' xmlns='http://www.test.com.au/docs/schemas' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.test.com/docs/schemas/spin/surcharge http://www.test.com/docs/schemas/test.xsd'>
                            <Parent id='1' name='parent1'>
                                <Child id='1' name='child1'>
                                    <Item id='1' name='someitem'></Item>
                                </Child>
                            </Parent>
                            <Parent id='2' name='parent2'>
                                <Child id='2' name='child2'>
                                    <Item id='2' name='someotheritem'></Item>
                                </Child>
                            </Parent>
                            </Root>
                            ");

XNamespace ns = element.Name.Namespace;

var list =
 from compileItem in element.Elements (ns + "Parent") 
 select new Parent
 {
    Id = compileItem.Attribute("id").Value.ToString(),
    Name = compileItem.Attribute("name").Value.ToString(),
    children = GetChild(compileItem)
            // this call here I'd like to replace with another linq select
 };

 public List<Child> GetChild(XElement frag)
 {
           //etc 
 }
 public List<Item> GetItem(XElement frag)
 { 
        //etc
 }

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    小控制台应用程序。老实说,它的功能更具可读性。

    public class Parent
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public List<Child> Childrens { get; set; }
        }
    
        public class Child
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public List<Item> Items { get; set; }
        }
        public class Item
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    
        internal class Program
        {
            private class ADSetupInformation
            {
                public static void Main()
                {
    
                    var element =
                        XElement.Parse(
                            @"<Root RegisterVersion='1.0' xmlns='http://www.test.com.au/docs/schemas' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.test.com/docs/schemas/spin/surcharge http://www.test.com/docs/schemas/test.xsd'>
                                <Parent id='1' name='parent1'>
                                    <Child id='1' name='child1'>
                                        <Item id='1' name='someitem'></Item>
                                    </Child>
                                </Parent>
                                <Parent id='2' name='parent2'>
                                    <Child id='2' name='child2'>
                                        <Item id='2' name='someotheritem'></Item>
                                    </Child>
                                </Parent>
                                </Root>
                                ");
    
                    XNamespace ns = element.Name.Namespace;
    
    
    var list =
                        element.Elements(ns + "Parent")
                            .Select(compileItem => new Parent
                                                       {
                                                           Id = Convert.ToInt32(compileItem.Attribute("id").Value),
                                                           Name = compileItem.Attribute("name").Value,
                                                           Childrens = compileItem.Elements(ns + "Child")
                                                               .Select(child => new Child
                                                                                    {
                                                                                        Id = Convert.ToInt32(child.Attribute("id").Value),
                                                                                        Name = child.Attribute("name").Value,
                                                                                        Items = child.Elements(ns + "Item")
                                                                                            .Select(xe => new Item()
                                                                                                              {
                                                                                                                  Id = Convert.ToInt32(xe.Attribute("id").Value),
                                                                                                                  Name = xe.Attribute("name").Value,
                                                                                                              }).ToList()
                                                                                    }).ToList()                             
                                                       });
    

    【讨论】:

    • 谢谢。我认为您对可读性的看法可能也是正确的,但知道如何做仍然很好。
    猜你喜欢
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多