数组对象的C#XML序列化对象”;生成XML文档时出错&”;

本文关键字:对象 文档 出错 XML 生成 C#XML 序列化 数组 | 更新日期: 2023-09-27 18:28:57

我的c#XML序列化系统有问题,它引发了一个Ambigus异常,

生成XML文档时出错。

现在我有一个类,它包含对其他类的引用和其他类的数组

E.G

namespace P2PFileLayout
{
    public class p2pfile
    {
        public FileList FileList;
        public StatusServer StatusServer;
        public String Hash;
    }
}
namespace P2PFileLayout.parts
{
    public class StatusServer
    {
        public Auth Auth;
        public Servers Servers;
    }
    public class Servers
    {
        public Server[] Server;
    }
    public class Server
    {
        public bool AuthRequired = false;
        public string Address;
    }
    public class Files
    {
        public File[] File;
    }
    public class File
    {
        public string FileName = "";
        public int BlockSize = 0;
        public int BlockCount = 0;
    }
    public class Directory
    {
        public string Name;
        public Files Files;
        public Directory[] Dir;
    }
    public class Auth
    {
        public AuthServer[] AuthServer;
    }
    public class FileList
    {
        public Files Files;
        public Directory[] Directory;
    }
}

我的示例数据

        // create the test file
        testFile = new p2pfile();
        // create a fake fileList
        testFile.FileList = new P2PFileLayout.parts.FileList();
        testFile.FileList.Directory = new P2PFileLayout.parts.Directory[1];
        testFile.FileList.Directory[0] = new P2PFileLayout.parts.Directory();
        testFile.FileList.Directory[0].Name = "testFolder";
        testFile.FileList.Directory[0].Files = new P2PFileLayout.parts.Files();
        testFile.FileList.Directory[0].Files.File = new P2PFileLayout.parts.File[2];
        testFile.FileList.Directory[0].Files.File[0] = new P2PFileLayout.parts.File();
        testFile.FileList.Directory[0].Files.File[0].FileName = "test.txt";
        testFile.FileList.Directory[0].Files.File[0].BlockSize = 64;
        testFile.FileList.Directory[0].Files.File[0].BlockCount = 1;
        testFile.FileList.Directory[0].Files.File[1] = new P2PFileLayout.parts.File();
        testFile.FileList.Directory[0].Files.File[1].FileName = "test2.txt";
        testFile.FileList.Directory[0].Files.File[1].BlockSize = 64;
        testFile.FileList.Directory[0].Files.File[1].BlockCount = 1;
        // create a fake status server
        testFile.StatusServer = new P2PFileLayout.parts.StatusServer();
        testFile.StatusServer.Servers = new P2PFileLayout.parts.Servers();
        testFile.StatusServer.Servers.Server = new P2PFileLayout.parts.Server[1];
        testFile.StatusServer.Servers.Server[0] = new P2PFileLayout.parts.Server();
        testFile.StatusServer.Servers.Server[0].Address = "http://localhost:8088/list.php";
        // create a file hash (real)
        HashGenerator.P2PHash hashGen = new HashGenerator.P2PHash();
        testFile.Hash = hashGen.getHash();
        treeView1.Nodes.Add(new TreeNode("Loading..."));
        Classes.CreateTreeView ctv = new Classes.CreateTreeView();
        ctv.BuildTreeView(testFile.FileList, treeView1);
        treeView1.AfterCheck += new TreeViewEventHandler(treeView1_AfterCheck);

现在,就深度而言,这并不像我的i循环对象那样复杂,所以dir支持更多的dir,但这只是的一个例子

然后我将序列化为字符串var,因为我想做的不仅仅是序列化它,但这里是我的序列化

private string ToXml(object Obj, System.Type ObjType)
        {
            // instansiate the xml serializer object
            XmlSerializer ser = new XmlSerializer(ObjType);
            // create a memory stream for XMLTextWriter to use
            MemoryStream memStream = new MemoryStream();
            // create an XML writer using our memory stream
            XmlTextWriter xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            // write the object though the XML serializer method using the W3C namespaces
            ser.Serialize(xmlWriter, Obj);
            // close the XMLWriter
            xmlWriter.Close();
            // close the memoryStream
            memStream.Close();
            // get the string from the memory Stream
            string xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            // remove the stuff before the xml code we care about
            xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            // clear any thing at the end of the elements we care about
            xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            // return the XML string
            return xml;
        }

有人能看到为什么它不起作用吗?或者它为什么不能正常工作的任何线索吗?

数组对象的C#XML序列化对象”;生成XML文档时出错&”;

为什么要手动执行?这种方法怎么样?http://support.microsoft.com/kb/815813

Test test = new Test() { Test1 = "1", Test2 = "3" };
System.Xml.Serialization.XmlSerializer x = new   System.Xml.Serialization.XmlSerializer(test.GetType());
MemoryStream ms = new MemoryStream();
x.Serialize(ms, test);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string xml = sr.ReadToEnd();

至于ambigus消息,请查看内部异常。XML序列化错误大量使用innerexception,您通常必须查看所有级别的innerexception才能知道实际发生了什么。