ARTICLE

Serializing and Deserializing XML String

Posted by Sonu Chauhan Articles | Visual C# July 20, 2006
In this article we will see how we can serialize a xml sring into an object and vice versa. You can serialize your object to a byte array and can deSerialize the byte array into an object.
Reader Level:
Download Files:
 

Serialization is handled by System.Runtime.Serialization namespace. To serialize an object, you need to create two things, stream to contain the serialized objects and a formatter to serialize the objects into the stream.

so here in this sample we will see how to serialize XML document into an object.

//This will returns the set of included namespaces for the serializer.
public static XmlSerializerNamespaces GetNamespaces()
{

XmlSerializerNamespaces ns;
ns =
new XmlSerializerNamespaces();
ns.Add("xs", "http://www.w3.org/2001/XMLSchema");
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
return ns;

}

 //Returns the target namespace for the serializer.
public static string TargetNamespace
{

Get
{

return http://www.w3.org/2001/XMLSchema;
}

}

 //Creates an object from an XML string.
public static object FromXml(string Xml, System.Type ObjType)
{

XmlSerializer ser;
ser =
new XmlSerializer(ObjType);
StringReader stringReader;
stringReader =
new StringReader(Xml);
XmlTextReader xmlReader;
xmlReader =
new XmlTextReader(stringReader);
object obj;
obj = ser.Deserialize(xmlReader);
xmlReader.Close();
stringReader.Close();
return obj;

}

 //Serializes the <i>Obj</i> to an XML string.
public static string ToXml(object Obj, System.Type ObjType)
{

XmlSerializer ser;
ser =
new XmlSerializer(ObjType, SerializeObject.TargetNamespace);
MemoryStream memStream;
memStream =
new MemoryStream();
XmlTextWriter xmlWriter;
xmlWriter =
new XmlTextWriter(memStream, Encoding.UTF8);
xmlWriter.Namespaces =
true;
ser.Serialize(xmlWriter, Obj, SerializeObject.GetNamespaces());
xmlWriter.Close();
memStream.Close();
string xml;
xml = Encoding.UTF8.GetString(memStream.GetBuffer());
xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
return xml;

}

Login to add your contents and source code to this article
post comment
     

The FromXml function (deserialization) throws an error as it is written. To resolve the error, you need to force the XmlSerializer to initialize with namespace information as such://Creates an object from an XML string.public static object FromXml(string Xml, System.Type ObjType){XmlSerializer ser;ser = new XmlSerializer(ObjType, ModuleHandler.TargetNamespace);

Posted by Jordan Aug 03, 2012

I keep getting the following error at runtime when trying to serialize web controls : The XML element 'EnableTheming' from namespace 'http://www.w3.org/2001/XMLSchema' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element. I've turned EnableTheming to false when adding controls to the page. I've tried using other namespaces. But this error prevents serialization of any Control object.

Posted by Bob Dickow Feb 15, 2009
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts