使用XML序列化保存TableLayoutPanel的内容 [英] Using XML serialization to save the contents of TableLayoutPanel

查看:78
本文介绍了使用XML序列化保存TableLayoutPanel的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经使用XML序列化将文本和图片保存在XML文件中.但是,我对如何进一步扩展该概念感到困惑.

我有第二个Windows窗体,其中有一个TableLayoutPanel,其中每个单元格都有一个图片框和一个文本框.用户可以自行设置行数和列数,因此我不知道必须保存在XML文件中的图片数.此外,当用户按下加载按钮时,相同的图片必须返回到TableLayout中的原始图片框.

如何将TableLayout的内容保存在XML文件中,并在其对应的单元格中检索它们?

这是我到目前为止所做的程序.

http://www.mediafire.com/file/7f4dax0szsiaf4g/WindowsFormsApplication4.rar

感谢您的帮助.

Hi,

I have used the XML serialization to save Text and a picture in a XML file. However, I am confused on how to extend the concept further.

I have a second Windows Form which has a TableLayoutPanel in which each cell has a Picture Box and a TextBox. The user has the option to set the number of rows and columns himself, so I do not know about the number of pictures that have to be saved in the XML file. Further, when the user pressed the load button, the same pictures have to come back to their original pictures boxes in the TableLayout.

How can I save the contents of the TableLayout in the XML file and retrieve them in their corresponding cells?

Here''s the program that I have done till now.

http://www.mediafire.com/file/7f4dax0szsiaf4g/WindowsFormsApplication4.rar

Thanks for help.

推荐答案

K,
我会写代码..

通过名称输出"向您的项目添加一个类,并添加以下代码

K,
just i ll write the code..

add a class to your project by name "Output" and add the below code

namespace YourNamespace
{
    class Output
    {
        int row;
        int column;
        List<Customer> person = new List<Customer>();
        public int Row
        {
            get { return row; }
            set { row = value; }
        }
        public int Column
        {
            get { return column; }
            set { column = value; }
        }
        public List<Customer> Person
        {
            get { return person; }
            set { person = value; }
        }
        public void Load(string path)
        {
            if (File.Exists(path))
            {
                Output o = ObjectXMLSerializer<Output>.Load(path);
                this.row = o.row;
                this.column = o.column;
                this.person = o.person;
            }
        }
        public void Save(string path)
        {
            ObjectXMLSerializer<Output>.Save(this, path);
        }
    }
}




然后按如下所示修改您的Form1(TableLayoutForm)类




then modify your Form1 (TableLayoutForm) class as follows

namespace MSDNForums
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Output output = new Output();

        private void button1_Click(object sender, EventArgs e)
        {
            output.Person.Clear();
            output.Row = int.Parse(rowsTextBox.Text);
            output.Column = int.Parse(columnsTextBox.Text);

            tableLayoutPanel1.RowCount = int.Parse(rowsTextBox.Text);
            tableLayoutPanel1.ColumnCount = int.Parse(columnsTextBox.Text);

            for (int col = 0; col < tableLayoutPanel1.ColumnCount; col++)

            {

                for (int rows = 0; rows < tableLayoutPanel1.RowCount; rows++)

                {

                    Panel p = new Panel();

                    TextBox tb = new TextBox();

                    PictureBox picb = new PictureBox();

                    p.Controls.Add(tb);

                    p.Controls.Add(picb);

                    picb.Location = new Point(0, tb.Top + 20);

                    tableLayoutPanel1.Controls.Add(p, col, rows);



                    Customer c = new Customer();

                    c.Index = col * tableLayoutPanel1.RowCount + rows;

                }

            }

        }



        private void LoadPerson()

        {

            output.Load("output.out");



            tableLayoutPanel1.Controls.Clear();



            foreach (Customer c in output.Person)

            {

                int rows = c.Index / output.Column;

                int col = c.Index % output.Column;



                Panel p = new Panel();

                TextBox tb = new TextBox();

                PictureBox picb = new PictureBox();

                p.Controls.Add(tb);

                p.Controls.Add(picb);

                picb.Location = new Point(0, tb.Top + 20);

                tableLayoutPanel1.Controls.Add(p, col, rows);

            }

        }



        private void SavePerson()

        {

            output.Save("output.out");

        }

    }

}



您可以使用SavePerson和LoadPerson函数来保存和加载文件.
如果您仍然没收到,最好将完整的代码发送给您:)



you can use SavePerson and LoadPerson function to save and load the file.
If you still didnt get, then better i ll send you the full code :)




方法1:

您可以向客户对象再添加一个可变索引(int),以跟踪客户在TableLayoutPanel中的位置(图片和文本).

这样您的客户类别就变成了
< pre>
公共类客户
{
int索引;
字符串CustomerName;
位图图片;
}
</pre>
那么您可以使用列表来存储添加到TableLayoutPanel中的用户界面对象.

列表<客户> customer_list;

在将列表保存为Xml(简单序列化)的同时,您可以根据TableLayoutPanel中的位置更改索引,并且可以保持跟踪.

方法2:

您可以使用包含位置和客户对象的Dictionary来代替向客户对象添加索引.

字典< int,客户>

问题是您无法通过XmlSerializer序列化字典.您必须创建自己的SerializableDictionary.我在自己的课堂上尝试过,并且有效.这是代码:

< pre>
[XmlRoot("dictionary")]
公共类MyDictionary& lt; TKey,TValue& gt;
:字典& lt; TKey,TValue,IXmlSerializable
{
公共System.Xml.Schema.XmlSchema GetSchema()
{
返回null;
}

公共无效ReadXml(System.Xml.XmlReader阅读器)
{
XmlSerializer keySerializer =新的XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer =新的XmlSerializer(typeof(TValue));

bool wasEmpty = reader.IsEmptyElement;
reader.Read();

如果(wasEmpty)
返回;

while(reader.NodeType!= System.Xml.XmlNodeType.EndElement)
{
reader.ReadStartElement("item");

reader.ReadStartElement("key");
TKey键=(TKey)keySerializer.Deserialize(阅读器);
reader.ReadEndElement();

reader.ReadStartElement("value");
TValue值=(TValue)valueSerializer.Deserialize(阅读器);
reader.ReadEndElement();

this.Add(key,value);

reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}

公共无效WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer keySerializer =新的XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer =新的XmlSerializer(typeof(TValue));

foreach(this.Keys中的TKey键)
{
writer.WriteStartElement("item");

writer.WriteStartElement("key");
keySerializer.Serialize(writer,key);
writer.WriteEndElement();

writer.WriteStartElement("value");
TValue值= this [key];
valueSerializer.Serialize(writer,value);
writer.WriteEndElement();

writer.WriteEndElement();
}
}
}
</pre>


希望对您有帮助...
Hi,

Method 1:

You can add one more variable index(int) to your customer object, which keep track of the position of the customer(picture and text) in your TableLayoutPanel.

so your customer class becomes
<pre>
public class Customer
{
int index;
string CustomerName;
Bitmap Picture;
}
</pre>
then you can use list to store the cutomer objects added to your TableLayoutPanel.

List<Customer> customer_list;

while saving the list in Xml (Simple Serialization) you can change the index according to the position in the TableLayoutPanel and you can keep track.

Method 2:

Instead of adding index to your customer object, you can use Dictionary which contains the position and customer object.

Dictionary<int, Customer>

The problem is you cant Serialize Dictionary by the XmlSerializer. You have to create you own SerializableDictionary. I tried with my own class and it works. here is the code :

<pre>
[XmlRoot("dictionary")]
public class MyDictionary&lt;TKey, TValue&gt;
: Dictionary&lt;TKey, TValue, IXmlSerializable
{
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}

public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

bool wasEmpty = reader.IsEmptyElement;
reader.Read();

if (wasEmpty)
return;

while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
{
reader.ReadStartElement("item");

reader.ReadStartElement("key");
TKey key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();

reader.ReadStartElement("value");
TValue value = (TValue)valueSerializer.Deserialize(reader);
reader.ReadEndElement();

this.Add(key, value);

reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}

public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

foreach (TKey key in this.Keys)
{
writer.WriteStartElement("item");

writer.WriteStartElement("key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();

writer.WriteStartElement("value");
TValue value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();

writer.WriteEndElement();
}
}
}
</pre>


Hope it helps you...


这篇关于使用XML序列化保存TableLayoutPanel的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆