John Doe
John Doe

Reputation: 10203

Serialize a PropertyGrid (SerializeToXML) failed

I try to serialize a PropertyGrid and write the results to a XML file. At the end the XML file is almost empty;
XML file

<?xml version="1.0" encoding="utf-8"?>
<MyBookCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />


My Code;

private void btnSave_Click(object sender, EventArgs e)
{
    MyBookCollection  MyBooks = new MyBookCollection ();

    SerializeToXML(MyBooks);
}

public void SerializeToXML(MyBookCollection MyBooks)
{
    XmlSerializer serializer = new XmlSerializer(typeof(MyBookCollection ));
    TextWriter textWriter = new StreamWriter(@"D:\BookInfo.xml");
    serializer.Serialize(textWriter, MyBooks);
    textWriter.Close();
}
}

[Serializable]
public class MyBookCollection 
{
     string m_Title;
     [Category("Book Titles")]
     [ReadOnly(true)]
     public string Title
     {
         get { return m_Title; }
         set { m_Caption = Title; }
     }
 }


What's wrong or how to do it better
Language: C#

Upvotes: 1

Views: 1707

Answers (2)

Larry
Larry

Reputation: 18041

Have you tried to get the .SelectedObject property this way ?

private void btnSave_Click(object sender, EventArgs e)
{
    var MyBooks = myProertyGrid.SelectedObject as MyBookCollection;
    SerializeToXML(MyBooks);
}

The PropertyGrid does unfortunately not bind property changes.

Upvotes: 1

deblendewim
deblendewim

Reputation: 425

I don't see what's wrong with the XML. Your XML serialization is ok!

You only see your MyBookCollection node because there is no other data present in the object!

Kind regards, Wim

Upvotes: 0

Related Questions