linguini
linguini

Reputation: 1939

Serilaization xml datagrid/textbox

Probably this may be very basic question but I'm working on a small example project for my understanding and I need some help here to finish this.

public class XMlExample : INotifyPropertyChanged
        {
            [XmlElement("ID")]
            public string ID { get; set; } //Textbox
            [XmlAttribute("Initial")]
            public string Initial { get; set; } //Textbox



            public event PropertyChangedEventHandler PropertyChanged;
        }
public class Details //Datagrid
                {
                    [XmlElement("FirstName")]
                    public string FirstName { get; set; }
                    [XmlElement("LastName")]
                    public string LastName { get; set; }
                }

This is the in-completed function:

Read Write Function: Button1 to read XML file:

 XmlSerializer deserializer = new XmlSerializer(typeof(XMlExample));
            TextReader textReader = new StreamReader(@"C:\test\testserialization.xml");
            XMlExample xmlexmaple;
            xmlexmaple = (XMlExample)deserializer.Deserialize(textReader);
            textReader.Close();

Button 2 to write XML file:

XmlSerializer serializer = new XmlSerializer(typeof(XMlExample));
            TextWriter textWriter = new StreamWriter(@"C:\test\testserialization.xml");
            serializer.Serialize(textWriter, XXXX);
            textWriter.Close();

Please somebody assist me that How can i get the values from textbox and datagrid to write as a xml file and how can i read that back to the interface. Thank you.

Upvotes: 0

Views: 583

Answers (1)

Dave M
Dave M

Reputation: 1322

To serialize/deserialize the Datagrid, you can add a property for it in your XMLExample Class and add the Serializable attribute to both classes.

[Serializable]
public class XMLExample : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public XMLExample()
    {
        ID = "Spaghetti";
        Initial = "Linguini";
        Details = new List<Detail>();
    }

    public string ID { get; set; } // Textbox
    public string Initial { get; set; } // Textbox
    public List<Detail> Details { get; set; } // Datagrid
}

[Serializable]
public class Detail 
{
    public Detail()
    {
        // default values, if appropriate.
        FirstName = "John"; 
        LastName = "Shaw";
    }

    [XmlElement("FirstName")]
    public string FirstName { get; set; }
    [XmlElement("LastName")]
    public string LastName { get; set; }
}

After deserializing, in your Button1 handler, you can populate your user interface controls appropriately. Before serializing the class, in your Button2 handler, you'll need to populate the properties in your class appropriately.

XmlSerializer _serializer = new XmlSerializer(typeof(XMLExample));
XMLExample _example = new XMLExample();

// Read file.
using (TextReader textReader = new StreamReader(@"C:\test\testserialization.xml"))
{
    _example = (XMLExample)_serializer.Deserialize(textReader);
    textReader.Close();
}

// Populate user interface from the class.
textBox1.Text = _example.ID;
textBox2.Text = _example.Initial;
// etc...

// Update class from user interface
_example.Details.Add(new Detail() { FirstName = "John", LastName = "Doe" });
_example.Details.Add(new Detail() { FirstName = "Jane", LastName = "Doe" });

// Write file.
using (TextWriter textWriter = new StreamWriter(@"C:\test\testserialization.xml"))
{
    _serializer.Serialize(textWriter, _example);
    textWriter.Close();
}

testserialization.xml

<?xml version="1.0" encoding="utf-8"?>
<XMLExample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ID>Spaghetti</ID>
  <Initial>Linguini</Initial>
  <Details>
    <Detail>
      <FirstName>John</FirstName>
      <LastName>Doe</LastName>
    </Detail>
    <Detail>
      <FirstName>Jane</FirstName>
      <LastName>Doe</LastName>
    </Detail>
  </Details>
</XMLExample>

Upvotes: 1

Related Questions