C#XML Serialize Array对象的对象“生成XML文档时出错”。

时间:2012-03-23 04:11:08

标签: c# xml-serialization

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

  

生成XML文档时出错。

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

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支持更多的dirs但这仅仅是一个例子

然后我将序列化为字符串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;
        }

任何人都可以理解为什么这不起作用或任何关于它为什么不能正常工作的线索<​​/ p>

2 个答案:

答案 0 :(得分:1)

你为什么要手动做? 这种方法怎么样? 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();

答案 1 :(得分:0)

至于暧昧的消息,请看一下内部异常。 XML序列化错误大量使用innerexception,您通常必须查看所有级别的InnerExceptions以了解实际发生的情况。