0

可能这可能是一个非常基本的问题,但我正在研究一个小示例项目以供我理解,我需要一些帮助来完成这个。

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

这是未完成的功能:

读写功能:Button1 读取 XML 文件:

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

按钮 2 写入 XML 文件:

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

请有人帮助我如何从文本框和数据网格中获取值以写入 xml 文件以及如何将其读回界面。谢谢你。

4

1 回答 1

1

要序列化/反序列化Datagrid,您可以在XMLExampleClass 中为它添加一个属性,并将该Serializable属性添加到两个类中。

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

反序列化后,在您的Button1处理程序中,您可以适当地填充您的用户界面控件。在序列化类之前,在您的Button2处理程序中,您需要适当地填充类中的属性。

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

测试序列化.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>
于 2012-10-13T13:35:57.430 回答