Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I tried code like this.....

//Serialization

private void Serialize_Click(object sender, EventArgs e)
        {
            List<Personal> pdetails = new List<Personal>();
            Personal personals = new Personal
            {
                ID = int.Parse(txtsno.Text),
                Name = txtname.Text,
                Phone = long.Parse(txtpno.Text),
                Address = txtaddr.Text
            };
            pdetails.Add(personals);

 XmlSerializer xmlser = new XmlSerializer(typeof(List<Personal>));
            StreamWriter swtr = new StreamWriter(@"f:\serialize.xml");
            xmlser.Serialize(swtr, pdetails);
            swtr.Close();
 }

//Deserialization

private void button3_Click(object sender, EventArgs e)
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(List<Personal>));
            StreamReader srdr = new StreamReader(@"f:\serialize.xml");
            List<Personal>p = (List<Personal>)xmlser.Deserialize(srdr);
            srdr.Close();
        }

but i want dynamic xml serialization and deserialization...

i.e. while i serializing the objects that want to add in xml doc.. two and more input datas....

but i entering the details that creating a xml file tooo... but my probem is cant enter another input data to the existing file itself....

For that i want to use memory stream.. How to use memory stream for write more inputs in xml by clicking the buttons..

how to do deserialization for get xml to objects...

share|improve this question
 
I can't understand your question. Do you want dynamic serialization? Method that would insert new Personal to existing List<Presonanl> serialized in xml file (method DynamicInsert(xml_file, PersonalObj))? –  Ari Jul 23 at 13:40
 
dynamic serialization means, In serialization, first time am entering details means no problem the object creating an xml file correctly.. Second time i entering details means its overwriting the data which was i entered first time data... In deserialization i getting error like this "There is an error in XML document (2, 2)." –  siva Jul 23 at 13:45
 
If you need to save data after closing and opening app I would recommend solution 1 from my answer. Otherwise you need to tell when and why you are serializing and deserializing data. –  Ari Jul 23 at 13:52

2 Answers

you have to have a base class to present your data and is an example:

  [XmlIncludeAttribute(typeof(ConcreteFooOne))]
  [XmlIncludeAttribute(typeof(ConcreteFooTwo))]
  [XmlIncludeAttribute(typeof(ConcreteFooThree))]
  [XmlRoot(ElementName = "FooData", Namespace = "http://foo.bar")]  
  public abstract partial class AbstractFoo
  {

  }

  [XmlRoot(ElementName = "FooData", Namespace = "http://foo.bar")]

  public class ConcreteFooOne : AbstractFoo
  {
    public string MyProp { get; set; }
  }
  [XmlRoot(ElementName = "FooData", Namespace = "http://foo.bar")]

  public class ConcreteFooTwo : AbstractFoo
  {

  }
  [XmlRoot(ElementName = "FooData", Namespace = "http://foo.bar")]

  public class ConcreteFooThree : AbstractFoo
  {

}


class Program
  {
    static void Main()
    {          

      XmlSerializer xs = new XmlSerializer(typeof(List<AbstractFoo>));
      TextWriter writer = new StreamWriter("test.txt", false);
      var list = new List<AbstractFoo>();
      list.Add(new ConcreteFooOne() { MyProp = "test" });
      list.Add(new ConcreteFooTwo() );
      list.Add(new ConcreteFooThree());
      xs.Serialize(writer, list);

      writer.Close();

      StreamReader srdr = new StreamReader("test.txt");
      var lst = (List<AbstractFoo>)xs.Deserialize(srdr);
      srdr.Close();          
    }
  }
share|improve this answer
 
thanks houssem.... But i cant get my solution from this.... i want to create xml file from serialization i.e. object to xml. In deserialization, xml to object. –  siva Jul 24 at 4:57

I think your problem is not with serialization itself but with synchronization.

I can see few possible solutions:

  1. Serialization on closing and deserialization on opening your app. You can move part with serializing to OnClose event for your form and part with deserializing to constructor. That way there is no problem with additional data added while serialization process is in progress

  2. Synchronization of process. You can block adding new data while serialization is still not completed. After that you can unblock. Another solution could be having copy of pdetails. That way you can serialize copy. Serialization would copy pdetails to pdetails_copy and serialize pdetails_copy and check if there is new data in pdetails. If so it would do serialization again.

share|improve this answer
 
AM i want to do the serialization and deserialization.. –  siva Jul 24 at 5:01
 
@siva I can't understand –  Ari Jul 24 at 10:05
 
I want sample coding for serialization and deserialization...for above mentioned code... –  siva Jul 24 at 10:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.