Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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,然后读取XML_C#_Xml_Xml Serialization_Xml Deserialization - Fatal编程技术网

C# 将列表转换为XML,然后读取XML

C# 将列表转换为XML,然后读取XML,c#,xml,xml-serialization,xml-deserialization,C#,Xml,Xml Serialization,Xml Deserialization,到目前为止,我已经编写了一个XML,它通过构造函数传递列表和一些其他有价值的信息,并将其保存为: RoundEdit._quizStruct.Add(new RoundEdit(quizId, roundId, roundName, QuestionsCount, Questions)); 这是构造函数,什么都不是 public RoundEdit() { quizStruct = new List<RoundEdit>();

到目前为止,我已经编写了一个XML,它通过构造函数传递列表和一些其他有价值的信息,并将其保存为:

 RoundEdit._quizStruct.Add(new RoundEdit(quizId, roundId, roundName, QuestionsCount, Questions));
这是构造函数,什么都不是

public RoundEdit()
        {
            quizStruct = new List<RoundEdit>();
        }
        public RoundEdit(int inQuizID, int inRoundId,string inRoundName, int inNumOfQuestions, List<int> inRoundQuestions)
        {
            QuizId = inQuizID;
            RoundId = inRoundId;
            roundName = inRoundName;
            numOfQuestions = inNumOfQuestions;
            roundQuestions = inRoundQuestions;

        }

        public static void saveRounds()
        {
            SaveXmlQuiz.SaveData(_quizStruct, "rounds.xml");
        }

我得到一个错误,XML文档(2,2)中有一个错误,我认为这是它如何读取存储在圆问题列表中的原因,但我不确定是否有人可以帮助?

我建议您使用
XDocument
类,如下所示:

var xDoc = XDocument.Load(filepath);
var roundEditXmlArr = xDoc.Element("ArrayOfRoundEdit").Elements("RoundEdit").ToArray(); // array or list but you know the exact number
现在您有了一个包含所需元素的数组。现在,您可以从以下项目中读取信息:

List<RoundEdit> roundEditList = new List<RoundEdit>();

for (var i = 0; i < roundEditXmlArr.Length; i++)
{
   var roundEdit = new RoundEdit(roundEditXmlArr[i].Element("_quizId").Value, [...]);
   roundEditList.Add(roundEdit);
} 
List roundEditList=new List();
对于(变量i=0;i
这段代码只是一个例子——实现肯定会更好,而且应该更好

很抱歉,我没有使用
XmlSerializer
的经验,因此我无法说出问题的确切位置

var xDoc = XDocument.Load(filepath);
var roundEditXmlArr = xDoc.Element("ArrayOfRoundEdit").Elements("RoundEdit").ToArray(); // array or list but you know the exact number
List<RoundEdit> roundEditList = new List<RoundEdit>();

for (var i = 0; i < roundEditXmlArr.Length; i++)
{
   var roundEdit = new RoundEdit(roundEditXmlArr[i].Element("_quizId").Value, [...]);
   roundEditList.Add(roundEdit);
}