Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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# C“数组对象的XML序列化对象”;生成XML文档时出错。”;_C#_Xml Serialization - Fatal编程技术网

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

C# C“数组对象的XML序列化对象”;生成XML文档时出错。”;,c#,xml-serialization,C#,Xml Serialization,我对c#XML序列化系统有问题,它抛出了一个Ambigus异常 生成XML文档时出错 现在我有了一个类,它包含对其他类的引用和其他类的数组 例如 我的示例数据 // create the test file testFile = new p2pfile(); // create a fake fileList testFile.FileList = new P2PFileLayout.parts.FileList();

我对c#XML序列化系统有问题,它抛出了一个Ambigus异常

生成XML文档时出错

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

例如

我的示例数据

        // 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);
就dept而言,它不像我的i循环对象那样复杂,因此dir支持更多dir,但这只是一个示例

然后我将序列化为一个字符串变量,因为我想做的不仅仅是序列化它,这里是我的序列化

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;
        }

有人能看出为什么这不起作用,或者有什么线索可以说明为什么它不能正常工作

为什么要手动操作? 这种方法怎么样?


至于模棱两可的消息,请查看内部异常。XML序列化错误经常使用innerexception,您通常必须查看所有级别的innerexception以了解实际发生的情况。

这应该是一个注释而不是答案,原因是我需要将内容保存到一个变量中。您可以使用您的类而不是我的测试类:-)@MartinBarker,这是你在标题中的问题的答案。。。某种程度上。请注意,很难从您的问题中猜测您“需要将内容保存到变量中”。您的代码确实执行了它所执行的操作,但您没有详细说明您希望此代码执行的操作。。。考虑用你的实际目标更新你的问题。@ Martin Barker -我试过你的代码,工作得很好。您提供的示例类需要进行位调整。@Martin Barker-请参阅我的编辑,您的成员变量声明不正确。@Sandy不要通过编辑来回答问题。我想知道为什么在看到你的评论之前它不会抛出任何异常。@L.B-我的编辑不是答案,它只是修复了编译时错误。标题中的实际错误消息仅在序列化时显示。
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;
        }
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();