Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何仅使用linq将xml解析为对象树?_C#_Linq - Fatal编程技术网

C# 如何仅使用linq将xml解析为对象树?

C# 如何仅使用linq将xml解析为对象树?,c#,linq,C#,Linq,我试图将xml解析为对象的层次结构,但我不确定如何正确地递归该层次结构。我确实有一个粗略的解决方案(不是一个好的解决方案),那就是调用GetChild解析一个XElement并返回一个集合。我希望有人知道如何在纯linq表达式中实现这一点,也就是说,将父子项关系填充到列表中,而不需要内联调用GetChild()之类的函数 谢谢 var element = XElement.Parse(@"<Root RegisterVersion='1.0' xmlns='http://www.test.

我试图将xml解析为对象的层次结构,但我不确定如何正确地递归该层次结构。我确实有一个粗略的解决方案(不是一个好的解决方案),那就是调用GetChild解析一个XElement并返回一个集合。我希望有人知道如何在纯linq表达式中实现这一点,也就是说,将父子项关系填充到列表中,而不需要内联调用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
 }
var-element=XElement.Parse(@)
");
XNamespace ns=element.Name.Namespace;
变量表=
来自element.Elements中的compileItem(ns+“父级”)
选择新的父项
{
Id=compileItem.Attribute(“Id”).Value.ToString(),
Name=compileItem.Attribute(“Name”).Value.ToString(),
children=GetChild(compileItem)
//我想用另一个linq select替换此呼叫
};
公共列表GetChild(XElement frag)
{
//等
}
公共列表GetItem(XElement frag)
{ 
//等
}

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

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()                             
                                                   });
公共类父类
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表子项{get;set;}
}
公营儿童
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表项{get;set;}
}
公共类项目
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
内部课程计划
{
私有类ADSetupInformation
{
公共静态void Main()
{
变量元素=
解析(
@"
");
XNamespace ns=element.Name.Namespace;
变量表=
元素。元素(ns+父元素)
.Select(compileItem=>new Parent
{
Id=Convert.ToInt32(compileItem.Attribute(“Id”).Value),
Name=compileItem.Attribute(“Name”).Value,
Childrens=compileItem.Elements(ns+“Child”)
.选择(子项=>新子项
{
Id=Convert.ToInt32(子属性(“Id”).Value),
Name=child.Attribute(“Name”).Value,
项目=子元素(ns+“项目”)
.Select(xe=>newitem()
{
Id=Convert.ToInt32(xe.Attribute(“Id”).Value),
Name=xe.Attribute(“Name”).Value,
})托利斯先生()
})托利斯先生()
});

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

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()                             
                                                   });
公共类父类
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表子项{get;set;}
}
公营儿童
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表项{get;set;}
}
公共类项目
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
内部课程计划
{
私有类ADSetupInformation
{
公共静态void Main()
{
变量元素=
解析(
@"
");
XNamespace ns=element.Name.Namespace;
变量表=
元素。元素(ns+父元素)
.Select(compileItem=>new Parent
{
Id=Convert.ToInt32(compileItem.Attribute(“Id”).Value),
Name=compileItem.Attribute(“Name”).Value,