C# 使用Serializable()从XML文件填写数组

C# 使用Serializable()从XML文件填写数组,c#,xml,serialization,C#,Xml,Serialization,我目前正在尝试使用[Serializable()]命令对象反序列化XML,但遇到了一些问题。以下是代码示例: using System; using System.Xml.Linq; using System.Xml.Serialization; using System.IO; namespace XmlManipulation { public class XmlManip { public static TestCasesList SerializeTes

我目前正在尝试使用[Serializable()]命令对象反序列化XML,但遇到了一些问题。以下是代码示例:

using System;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;

namespace XmlManipulation
{
    public class XmlManip
    {
        public static TestCasesList SerializeTest() //test function
        {
            TestCasesList testslist = null;
            string path = @"C:\Users\sxtx2987\Documents\Visual Studio 2015\Projects\FDB\deleteme\test.xml";

            XmlSerializer serializer = new XmlSerializer(typeof(TestCasesList));

            StreamReader reader = new StreamReader(path);

        testslist = (TestCasesList)serializer.Deserialize(reader);
        reader.Close();
        return testslist;
        }
    }


    [Serializable()]
    public class TestCase
    {
        [System.Xml.Serialization.XmlElement("name")]
        public string name;

        [System.Xml.Serialization.XmlElement("objective")]
        public string objective;

        [System.Xml.Serialization.XmlElement("criteria")]
        public string criteria;

        [System.Xml.Serialization.XmlElement("issue")]
        public string issue;

        [System.Xml.Serialization.XmlElement("clientcomments")]
        public string clientcomments;

        [System.Xml.Serialization.XmlElement("authoritycomments")]
        public string authoritycomments;
    }

    [Serializable()]
    [XmlType("testcaseslist")]
    public class TestCasesList
    {
        [XmlArray("testcase")]
        public TestCase[] TestCase { get; set; }
    }
}
这是我试图反序列化的文件

<xml version="1.00" encoding="UTF-8">
    <campaign>
        <name>Test XML</name>
        <number>XXXXXG04-06-1</number>
        <client>Fictional</client>
        <model>Rocket powered boots</model>
        <authority>Fictional authority</authority>
    </campaign>

    <testcaseslist>
        <testcase>
            <name>TestCase001</name>
            <objective>Objective test 1</objective>
            <criteria>Pass criteria test 1</criteria>
            <issue>Issue Description test 1</issue>
            <clientcomments>Client comments test 1</clientcomments>
            <authoritycomments>Authority comments test 1</authoritycomments>
        </testcase>

        <testcase>
            <name>TestCase002</name>
            <objective>Objective test 2</objective>
            <criteria>Pass criteria test 2</criteria>
            <issue>Issue Description test 2</issue>
            <clientcomments>Client comments test 2</clientcomments>
            <authoritycomments>Authority comments test 2</authoritycomments>
        </testcase>
    </testcaseslist>    
</xml>
我得到一个空的测试对象

我认为我的问题是XmlType和XmlArray部分,但经过几个小时的研究和试验,我仍然无法从XML中获得信息


谢谢你抽出时间

这里有一些问题

  • 根元素可能不是您所认为的。您的代码假定
    是根元素。但实际上,以下是根本要素:

    <xml version="1.00" encoding="UTF-8">
        <!-- XML Contents -->
    </xml>
    
    然后,要在XML中向前跳并反序列化
    元素,请使用:

    你可以做:

        public static TestCasesList Deserialize(TextReader reader)
        {
            var serializer = new XmlSerializer(typeof(XmlRoot));
            var root = (XmlRoot)serializer.Deserialize(reader);
            return root == null ? null : root.testcaseslist;
        }
    
    通过从XML自动生成c#类,可以避免上述一些困难,例如使用。有关更多信息,请参阅


    顺便说一句,
    [Serializable]
    XmlSerializer
    没有影响

    谢谢你的详细解释。在搞乱了XML之后,我注意到当我使用这种结构进行序列化时,我可以简单地使用变量名,使用没有[serializable]标记的结构,并简单地使用公共变量进行序列化。不过,您的解释为我解释了XML的机制。非常感谢。
    <?xml version="1.0" encoding="UTF-8">
    
    [XmlRoot("testcaseslist")]
    public class TestCasesList
    {
        [XmlElement("testcase")]
        public TestCase[] TestCase { get; set; }
    }
    
        public static TestCasesList Deserialize(TextReader reader)
        {
            using (var xmlReader = XmlReader.Create(reader))
            {
                // Skip to the <testcaseslist> element.
                if (!xmlReader.ReadToFollowing("testcaseslist", ""))
                    return null;
    
                var serializer = new XmlSerializer(typeof(TestCasesList));
                return (TestCasesList)serializer.Deserialize(xmlReader);
            }
        }
    
    [XmlRoot("xml")]
    public class XmlRoot
    {
        public TestCasesList testcaseslist { get; set; }
    }
    
        public static TestCasesList Deserialize(TextReader reader)
        {
            var serializer = new XmlSerializer(typeof(XmlRoot));
            var root = (XmlRoot)serializer.Deserialize(reader);
            return root == null ? null : root.testcaseslist;
        }