0

I'm having a problem with the c# XML serialization system it's throws an Ambigus exception,

There was an error generating the XML document.

Now i have a Class that contains refferences to other classes and arrays of the other classes

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

My Example data

        // 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);

Now that is not as complicated as mine in terms of dept as my i loop objects so dir has support for more dirs but thats just an example

Then i'm serializing to a string var as i want to do a little more than just serialize it but here is my serialization

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

Can any one see why this is not working or any clues as to why it would not work normally

8
  • 1
    Your code does exactly what it does, but you have no detail what you want this code to do... Consider updating your question with your actual goal. Commented Mar 23, 2012 at 4:30
  • @Martin Barker - I tried your code and worked perfectly. The sample classes your provided need bit adjustments. Commented Mar 23, 2012 at 4:39
  • @Martin Barker - see my edit, your member variables declaration was incorrect. Commented Mar 23, 2012 at 4:41
  • @Sandy don't answer the question by editing it. I was wondering why it doesn't throw any exception till I saw your comment. Commented Mar 23, 2012 at 7:55
  • @L.B - my edit isn't the answer, its only fixes the compile time error. Actual error message in the title will only show up while serialization. Commented Mar 23, 2012 at 8:36

2 Answers 2

1

Why are you doing it manually? What about this approach? 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();
Sign up to request clarification or add additional context in comments.

3 Comments

That should be a comment not an answer, and the reason is in the question i need to save the content into a variable
You could use your class instead of my Test class :-)
@MartinBarker, this is answer to your question in the title... sort of. Note that it is very hard to guess that you "need to save the content into a variable" from your question.
0

As for the ambigous message, take a look into the inner exceptions. XML serialization errors use innerexception a lot, and you usually have to look to all levels of InnerExceptions to know what is really happening.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.