-1

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...

kasim
  • 346
  • 2
  • 5
  • 23
  • I can't understand your question. Do you want dynamic serialization? Method that would insert new `Personal` to existing `List` serialized in xml file (method DynamicInsert(xml_file, PersonalObj))? – Ari Jul 23 '13 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)." – kasim Jul 23 '13 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 '13 at 13:52

2 Answers2

1

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();          
    }
  }
Swift
  • 1,861
  • 14
  • 17
  • 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. – kasim Jul 24 '13 at 04:57
1

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.

Ari
  • 3,101
  • 2
  • 27
  • 49