TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Rahul Ray
NA
388
190.9k
Xml serialization and Deserialization
Mar 8 2010 12:48 AM
Xml Serialization
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.IO;
using
System.Xml;
using
System.Xml.Serialization;
public
class
Serialize
<T>
{
///
<summary>
///
Used to serialize and then return xml string
///
</summary>
///
<param name="obj">
T type
</param>
///
<returns>
String type
</returns>
public
static
string
SerializationObject(T obj)
{
try
{
string
xmlstring =
string
.Empty;
MemoryStream
memory =
new
MemoryStream
();
XmlSerializer
serialzer =
new
XmlSerializer
(
typeof
(T));
XmlTextWriter
writer =
new
XmlTextWriter
(memory,
Encoding
.ASCII);
serialzer.Serialize(writer, obj);
memory = (
MemoryStream
)writer.BaseStream;
ASCIIEncoding
encoding =
new
ASCIIEncoding
();
xmlstring = encoding.GetString(memory.ToArray());
return
xmlstring;
}
catch
(
Exception
ex)
{
return
string
.Empty;
}
}
///
<summary>
///
Used to Deserialize xml string and then return object
///
</summary>
///
<param name="XmlString">
string xml serialized value
</param>
///
<returns>
Deserialized object
</returns>
public
static
T DeserializationXMLString(
string
xmlString)
{
T obj =
default
(T);
try
{
XmlSerializer
xmlSerializer =
new
XmlSerializer
(
typeof
(T));
StringReader
stringReader =
new
StringReader
(xmlString);
XmlTextReader
xmlReader =
new
XmlTextReader
(stringReader);
obj = (T)xmlSerializer.Deserialize(xmlReader);
xmlReader.Close();
stringReader.Close();
}
catch
(
Exception
ex)
{ }
return
obj;
}
/*Call the class*/
For serilization
Serialize
<class1> serialize=new
Serialize
<class1>();
string xml=serialize.SerializationObject(class1 obj);
for deserialize
Serialize
<class1> serialize=new
Serialize
<class1>();
class1 ca=new class1 ();
ca=serialize.DeserializationXMLString(xml);
Reply
Answers (
0
)
Cross thread error in filewatcher
calculation with validation