Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one serialize an XDocument object?

I want to serialize an XDocument object. I wrote this code.

        XDocument signup_xml_file = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XComment("signup_xml_file"),
            new XElement("Student",
                new XElement("univ_id", univ_id),
                new XElement("personal_id",personal_id),
                new XElement("user_name", user_name)));
        client.Connect(host_name, port);
        //connect to the server .
        bf.Serialize(client.GetStream(), signup_xml_file); // serialize the signup_xml_file

I get the following exception when attempting to serialize the XDocument. Is there any way to make the XDocument class Serializable, or is there another way to send my XDocument?

Type 'System.Xml.Linq.XDocument' in Assembly 'System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

like image 210
user983195 Avatar asked Oct 09 '11 21:10

user983195


People also ask

What is serialize in XML?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.


1 Answers

XDocuments are not intended to be serialized. In a way they are serializers themselves.

But you can simply write them: signup_xml_file.Save(client.GetStream());
which also eliminates serializer overhead.

Edit:

And on the other side you will need

var doc = XDocument.Load(someStream);
like image 172
Henk Holterman Avatar answered Sep 21 '22 10:09

Henk Holterman