Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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# 如何将XML反序列化为特定结构(其中包含集合)_C#_Xml_Serialization - Fatal编程技术网

C# 如何将XML反序列化为特定结构(其中包含集合)

C# 如何将XML反序列化为特定结构(其中包含集合),c#,xml,serialization,C#,Xml,Serialization,以下是XML: <columninfo name="f1config"> <column name="col1,col2" fieldname="c1" type="string" /> <column name="col3" fieldname="c3" type="date" format="ddMMMyyy"/> <column name="col4,col5" fieldname="c4" type="string" fo

以下是XML:

<columninfo name="f1config">
    <column name="col1,col2" fieldname="c1" type="string" />
    <column name="col3" fieldname="c3" type="date" format="ddMMMyyy"/>
    <column name="col4,col5" fieldname="c4" type="string" format="ddMMMyyy"/>
 </columninfo>

问题是收集对象-
字段列表

尝试类似的方法

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

namespace ConsoleApplication48
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            columninfo.info = doc.Descendants("columninfo").Select(x => new columninfo() {
                name = (string)x.Attribute("name"),
                fieldList = columninfo.GetDictionary(x)
            }).ToList();

        }
    }
    public class columninfo
    {
        public static List<columninfo> info = new List<columninfo>();
        public string name {get;set;}
        public Dictionary<string,column> fieldList {get;set;}

        public static Dictionary<string,column> GetDictionary(XElement columninfo)
        {
            Dictionary<string,column> dict = new Dictionary<string,column>();

            foreach(XElement column in columninfo.Elements("column"))
            {
                string fieldname = (string)column.Attribute("fieldname");
                string _type = (string)column.Attribute("type");
                string name = (string)column.Attribute("name");

                column newCol;
                string[] names;
                switch(fieldname)
                {
                    case "c1" :
                        names = name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        newCol = new column() { name = names[0], format = _type, fieldname = fieldname };
                        dict.Add("c1", newCol);
                        newCol = new column() { name = names[1], format = _type, fieldname = fieldname };
                        dict.Add("c2", newCol);
                        break;

                    case "c3":
                        newCol = new column() { name = fieldname, format = _type, fieldname = fieldname };
                        dict.Add("c3", newCol);
                        break;

                    case "c4":
                        names = name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        newCol = new column() { name = names[0], format = _type, fieldname = fieldname };
                        dict.Add("c4", newCol);
                        newCol = new column() { name = names[1], format = _type, fieldname = fieldname };
                        dict.Add("c5", newCol);
                        break;
                }
            }

            return dict;
        }
    }

    public class column
    {
        public string name { get; set; }
        public string fieldname { get; set; }
        public string format { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序48
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
columninfo.info=doc.substands(“columninfo”)。选择(x=>newcolumninfo(){
名称=(字符串)x.Attribute(“名称”),
fieldList=columninfo.GetDictionary(x)
}).ToList();
}
}
公共类columninfo
{
公共静态列表信息=新列表();
公共字符串名称{get;set;}
公共字典字段列表{get;set;}
公共静态字典GetDictionary(XElement columninfo)
{
Dictionary dict=新字典();
foreach(columninfo.Elements(“列”)中的XElement列)
{
string fieldname=(string)column.Attribute(“fieldname”);
string _type=(string)column.Attribute(“type”);
字符串名称=(字符串)column.Attribute(“名称”);
新柱;
字符串[]名称;
交换机(字段名)
{
案例“c1”:
name=name.Split(新字符[]{',},StringSplitOptions.RemoveEmptyEntries);
newCol=newcolumn(){name=names[0],format=_type,fieldname=fieldname};
dict.Add(“c1”,newCol);
newCol=newcolumn(){name=names[1],format=_type,fieldname=fieldname};
dict.Add(“c2”,newCol);
打破
案例“c3”:
newCol=newcolumn(){name=fieldname,format=\u type,fieldname=fieldname};
dict.Add(“c3”,newCol);
打破
案例“c4”:
name=name.Split(新字符[]{',},StringSplitOptions.RemoveEmptyEntries);
newCol=newcolumn(){name=names[0],format=_type,fieldname=fieldname};
dict.Add(“c4”,新词);
newCol=newcolumn(){name=names[1],format=_type,fieldname=fieldname};
添加(“c5”,新词);
打破
}
}
返回命令;
}
}
公共类专栏
{
公共字符串名称{get;set;}
公共字符串字段名{get;set;}
公共字符串格式{get;set;}
}
}

我分为两个阶段,1)反序列化到包含数组的对象,2)然后通过linq转换到字典


谢谢

使用您当前的XML,您无法做到这一点。您可以通过转到Visual Studio>>编辑>>粘贴特殊>>将XML粘贴为类来验证它。在这里,您将看到,您定义的类层次结构与visual Studio生成的不同。我刚刚完成了这项工作,它显示了XML列数组。但是需要
字典
。如果这是一个问题,那么如何序列化/反序列化字典还有很多问题(例如)。至于您的问题,您总是可以使用xml。它是否同时具有-dictionary和name属性-
public string name{get;set;}public dictionary fieldList{get;set;}
作为上述xml的用法?@dsi您必须查看Sinatr注释。。否则,使用标准XML解析就没有解决方案。您必须手动完成。谢谢。实际上,我们已经应用了上面添加的答案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication48
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            columninfo.info = doc.Descendants("columninfo").Select(x => new columninfo() {
                name = (string)x.Attribute("name"),
                fieldList = columninfo.GetDictionary(x)
            }).ToList();

        }
    }
    public class columninfo
    {
        public static List<columninfo> info = new List<columninfo>();
        public string name {get;set;}
        public Dictionary<string,column> fieldList {get;set;}

        public static Dictionary<string,column> GetDictionary(XElement columninfo)
        {
            Dictionary<string,column> dict = new Dictionary<string,column>();

            foreach(XElement column in columninfo.Elements("column"))
            {
                string fieldname = (string)column.Attribute("fieldname");
                string _type = (string)column.Attribute("type");
                string name = (string)column.Attribute("name");

                column newCol;
                string[] names;
                switch(fieldname)
                {
                    case "c1" :
                        names = name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        newCol = new column() { name = names[0], format = _type, fieldname = fieldname };
                        dict.Add("c1", newCol);
                        newCol = new column() { name = names[1], format = _type, fieldname = fieldname };
                        dict.Add("c2", newCol);
                        break;

                    case "c3":
                        newCol = new column() { name = fieldname, format = _type, fieldname = fieldname };
                        dict.Add("c3", newCol);
                        break;

                    case "c4":
                        names = name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        newCol = new column() { name = names[0], format = _type, fieldname = fieldname };
                        dict.Add("c4", newCol);
                        newCol = new column() { name = names[1], format = _type, fieldname = fieldname };
                        dict.Add("c5", newCol);
                        break;
                }
            }

            return dict;
        }
    }

    public class column
    {
        public string name { get; set; }
        public string fieldname { get; set; }
        public string format { get; set; }
    }
}