Created
October 26, 2011 22:08
-
-
Save ivasilov/1318041 to your computer and use it in GitHub Desktop.
XML serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Person Deserialize(string file) | |
{ | |
XmlSerializer serializer = new XmlSerializer(typeof(Person)); | |
Stream reader = new FileStream("myXmFile.xml", FileMode.Open); | |
Person temp=(Person)serializer.Deserialize(reader); | |
reader.Close(); | |
return temp; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Serialize() | |
{ | |
XmlSerializer serializer = new XmlSerializer(typeof(Person)); | |
Stream writer = new FileStream("myXmFile.xml", FileMode.Create); | |
serializer.Serialize(writer, this); | |
writer.Close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Person Deserialize(string file) | |
{ | |
XmlSerializer serializer = new XmlSerializer(typeof(Person)); | |
Stream reader = new FileStream("C:\\myXmFile.xml", FileMode.Open); | |
Person temp=(Person)serializer.Deserialize(reader); | |
reader.Close(); | |
return temp; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Serialize() | |
{ | |
XmlSerializer serializer = new XmlSerializer(typeof(Person)); | |
Stream writer = new FileStream("C:\\myXmFile.xml", FileMode.Create); | |
serializer.Serialize(writer, this); | |
writer.Close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person | |
{ | |
public string FirstName; | |
public string LastName; | |
public int SSNo; | |
… | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Serialize() | |
{ | |
XmlTextWriter textWriter = new XmlTextWriter("myXmFile.xml", null); | |
// Opens the document | |
textWriter.WriteStartDocument(); | |
// Write first element | |
textWriter.WriteStartElement("Person"); | |
// Write next element | |
textWriter.WriteStartElement("Name", ""); | |
textWriter.WriteString(Firstname); | |
textWriter.WriteEndElement(); | |
... | |
textWriter.WriteEndDocument(); | |
// close writer | |
textWriter.Close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment