2015-09-16 69 views
0

我試圖在單擊按鈕時創建XML節點。問題是當我點擊保存按鈕時,它會覆蓋值&不爲每個項目生成節點。請告訴我最好的&簡單的過程來讀取&將文本框的值寫入到WPF中的XML。 請告訴我什麼是替代Form_Load WPF將文本框值寫入每個項目的XML文件

public class SaveXML 
{ 
    public static void SaveData(object obj, string filename) 
    { 
     XmlSerializer sr = new XmlSerializer(obj.GetType()); 
     XmlSerializer rs = new XmlSerializer(obj.GetType()); 
     TextWriter writer = new StreamWriter(filename); 
     sr.Serialize(writer, obj); 
     writer.Close(); 
    } 
} 

public class Products 
{ 
    private string DatePick; 
    private string name; 
    private string category; 
    private string volume; 
    private string volunit; 
    private string quantity; 
    private string purprice; 
    private string sellprice; 

    public Products() { } 
    public Products(string DatePick, string name, string category, string volume, string volunit, string quantity, string purprice, string sellprice) 
    { 
     this.DatePick = DatePick; 
     this.name = name; 
     this.category = category; 
     this.volume = volume; 
     this.volunit = volunit; 
     this.quantity = quantity; 
     this.purprice = purprice; 
     this.sellprice = sellprice; 
    } 

    public string DatePicker 
    { 
     get {return DatePick;} 
     set {DatePick = value;} 
    } 

    public string Name 
    { 
     get {return name;} 
     set {name = value;} 
    } 

    public string Category 
    { 
     get {return category;} 
     set {category = value;} 
    } 

    public string Volume 
    { 
     get {return volume;} 
     set {volume = value;} 
    } 

    public string VolUnit 
    { 
     get {return volunit;} 
     set {volunit = value;} 
    } 

    public string Quantity 
    { 
     get {return quantity;} 
     set {quantity = value;} 
    } 

    public string PurPrice 
    { 
     get {return purprice;} 
     set {purprice = value;} 
    } 

    public string SellPrice 
    { 
     get {return sellprice;} 
     set { sellprice = value;} 
    } 
} 

}`

private void Save_btn_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      Products product = new Products(); 

      product.DatePicker = Date_Picker.Text; 
      product.Name = Product_Name.Text; 
      product.Category = Cat.Text; 
      product.Volume = Vol.Text; 
      product.VolUnit = VolUnit.Text; 
      product.Quantity = Quantity.Text; 
      product.PurPrice = Pur_Price.Text; 
      product.SellPrice = Sell_Price.Text; 

      SaveXML.SaveData(product, "Data.xml"); 
     } 

     catch (Exception ex) 
     { 
      System.Windows.MessageBox.Show(ex.Message); 
     } 


    } 

    private void Main_Loaded(object sender, RoutedEventArgs e) 
    { 
     if (File.Exists("Data.xml")) 
     { 
      XmlSerializer xs = new XmlSerializer(typeof(Products)); 
      FileStream read = new FileStream("Data.xml", FileMode.Open,   FileAccess.Read, FileShare.Read); 
      Products product = (Products)xs.Deserialize(read); 

     } 
    } 
+0

我想再次和再次執行XML不想覆蓋values.I不想被覆蓋我的XML文件。任何幫助將不勝感激。 –

+0

請參閱我的評論每StreamWriter。請注意,它可以提高一次以上。 – Giangregorio

回答

相關問題