设置新的XML属性值C#

时间:2016-03-21 01:48:53

标签: c# xml serialization

我试图通过这种方式创建的XML文件更新属性值:

    [Serializable]
    public class Song
    {
        public string artist { get; set; }
        public string tittle { get; set; }
    }

    // Main class
    private void bt_save_Click(object sender, EventArgs e)
    {
        Song s = new Song("Chopin", "Nocturne B minor");
        SaveParametresXml(ref s);
    }

    public void SaveParametresXml(ref Song s)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Song));
        StreamWriter xml = new StreamWriter(@"D:\Tests\mysong.xml", false);
        serializer.Serialize(xml, s);
        xml.Close();
    }

哪个工作正常,我的XML文件得到以下结果:

    <?xml version="1.0" encoding="utf-8"?>
    <Song xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <artist>Chopin</artist>
      <tittle>Nocturne B minor</tittle>
    </Song>

问题在于我没有设法进行价值修改。 有没有人帮我处理我的XML问题?我已经尝试Deserialize,但我真的不知道应该如何使用它?

        public Song LoadXml(string path)
        {
            Song s = new Song();
            XmlSerializer xs = new XmlSerializer(typeof(Song));   
            StreamReader xml = new StreamReader(path);     
            s = (Song)xs.Deserialize(xml);                         
            xml.Close();                                          
            return s;                                           
        }

0 个答案:

没有答案