序列化PropertyGrid(SerializeToXML)失败

时间:2013-02-26 15:27:35

标签: c# xmlserializer propertygrid

我尝试序列化PropertyGrid并将结果写入XML文件。最后,XML文件几乎为空;
XML文件

<?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" />


我的代码;

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; }
     }
 }


错误或如何做得更好
语言:C#

2 个答案:

答案 0 :(得分:1)

您是否尝试过以这种方式获取.SelectedObject属性?

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

遗憾的是,PropertyGrid没有绑定属性更改。

答案 1 :(得分:0)

我没有看到XML有什么问题。 你的XML序列化还可以!

您只能看到MyBookCollection节点,因为对象中没有其他数据!

亲切的问候, 维姆