Skip to content

Instantly share code, notes, and snippets.

@ivasilov
Created October 26, 2011 22:08
Show Gist options
  • Save ivasilov/1318041 to your computer and use it in GitHub Desktop.
Save ivasilov/1318041 to your computer and use it in GitHub Desktop.
XML serialization
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;
}
public void Serialize()
{
XmlSerializer serializer = new XmlSerializer(typeof(Person));
Stream writer = new FileStream("myXmFile.xml", FileMode.Create);
serializer.Serialize(writer, this);
writer.Close();
}
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;
}
public void Serialize()
{
XmlSerializer serializer = new XmlSerializer(typeof(Person));
Stream writer = new FileStream("C:\\myXmFile.xml", FileMode.Create);
serializer.Serialize(writer, this);
writer.Close();
}
public class Person
{
public string FirstName;
public string LastName;
public int SSNo;
}
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