2012-12-13 41 views
3

我正在构建一个应用程序,它将使用文本框输入来编辑xml文件的内容。我已经获得了使用C#创建新xml文件的基本概念,但在尝试更新文件时,我仍然遇到错误。请帮忙?用C#和WPF更新XML文件

(我是谁需要大量的帮助下,新的开发人员,如果我需要发布其他任何东西,请添加注释。)

编辑:这是我的问题是:doc.CreateElement()。它不会更新文件。如果我点击保存按钮,文件本身不会更新。

这是我已经试过:

filename.xml中

<ApplicationPreferences> 
    <Facility>2400</Facility> 
    <FacilityName>Somewhere</FacilityName> 
</ApplicationPreferences> 

代码

namespace WIPSControlPanel 
{ 
[Serializable] 
public class ApplicationPreferences 
{ 
    private const string CONFIG_FILE_NAME = "fileName.xml"; 
    private string _facility = String.Empty; 
    private string _facilityName = String.Empty; 
    private string _preferencesPath = CONFIG_FILE_NAME; 
    private string _appURI = String.Empty; 

    [XmlElement] 
    public string FacilityName 
    { 
     get { return _facilityName; } 
     set { _facilityName = value; } 
    } 

    [XmlElement] 
    public string Facility 
    { 
     get { return _facility; } 
     set { _facility = value; } 
    } 

    public string AppURI 
    { 
     get { return _appURI; } 
    } 

    //Constructor 
    public ApplicationPreferences() 
    { 
    } 

    /// <summary> 
    /// Constructs the class 
    /// </summary> 
    public ApplicationPreferences(string pathName) 
    { 

     _preferencesPath = Path.Combine(pathName, CONFIG_FILE_NAME); 

    } 

    public void Save() 
    { 
     XmlSerializer serializer = new XmlSerializer(typeof(ApplicationPreferences)); 
     XmlWriter writer = GetXmlWriter(); 
     serializer.Serialize(writer, this); 
    } 

    public void Load() 
    { 
     XmlSerializer serializer = new XmlSerializer(typeof(ApplicationPreferences)); 
     XmlReader reader = GetXmlReader(); 
     ApplicationPreferences newConfig = (ApplicationPreferences)serializer.Deserialize(reader); 


     if (newConfig != null) 
     { 
      this.FacilityName = newConfig.FacilityName; 
      this.Facility = newConfig.Facility; 
     } 
    } 

private XmlReader GetXmlReader() 
    { 
     XmlReaderSettings settings = new XmlReaderSettings(); 

     return XmlReader.Create(_preferencesPath, settings); 
    } 


    private XmlWriter GetXmlWriter() 
    { 
     if (CONFIG_FILE_NAME != null) 
     { 
      XmlWriterSettings settings = new XmlWriterSettings(); 

      return XmlWriter.Create(Path.Combine(Path.GetDirectoryName(@"\\server"), CONFIG_FILE_NAME)); 
     } 
    } 
} 


namespace WIPSControlPanel 
{ 

    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
    #region Private Variables 
    private ApplicationPreferences _configFile = new ApplicationPreferences(); 
    } 

    public ApplicationPreferences ConfigFile 
    { 
     get { return _configFile; } 

     set 
     { 
      _configFile = value; 
      OnPropertyChanged("ConfigFile"); 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     Loaded += fileLoad; 
     this.DataContext = this; 
     this.ConfigFile = new ApplicationPreferences(@"\\server"); 
     this.ConfigFile.Load(); 
     LoadAvailableOperations(this.ConfigFile.Facility); 
     UpdateOperationDescriptions(this.ConfigFile); 
    } 

    private void butGenSave_Click(object sender, RoutedEventArgs e) 
    { 
     XmlDocument doc = new XmlDocument(); 
     doc.LoadXml("WIPSConfig"); 

     XmlWriter writer; 

     XElement newElement = doc.CreateElement(
      new XElement("Facility", txtFacility.Text), 
      new XElement("FacilityName", txtFacilityName.Text), 
      new XElement("ApplicationStatus", txtStatus.Text), 
      (new XElement("RefreshInterval", txtRefreshInterval.Text))); 
     doc.Save(writer); 

     writer = new XmlTextWriter("WIPSConfig.xml", null); 
     writer.WriteStartElement("ApplicationPreferences"); 
     writer.WriteEndElement(); 
     doc.Save(writer); 
     writer.Close(); 

    } 
} 

XAML的文本框

<TextBox x:Name="txtFacilityName" 
             Grid.Column="1" 
             Margin="5,5,0,5" 
             Width="129" 
             HorizontalAlignment="Left" 
             Text="{Binding ConfigFile.FacilityName}" /> 
<TextBox x:Name="txtFacility" 
             Grid.Column="1" 
             Grid.Row="1" 
             Width="129" 
             Margin="5,5,0,5" 
             HorizontalAlignment="Left" 
             Text="{Binding ConfigFile.Facility}" /> 
+5

你得到什么样的错误? – jaredk

+0

你不能将'System.Xml.CreateElement()'的结果赋值给'System.Xml.Linq.XElement',它们是完全不同的类。 'LoadXml()'需要一个XML字符串,而“WIPSConfig”肯定不是。你试着创建一个'XElement',然后把它扔掉而不用做任何事情。然后你尝试反序列化一个XmlReader,它不是一个可序列化的类。请告诉我们你想要达到的目标,而不是你想要做的,因为你试图做的事情没有意义。 –

回答

0

了蝙蝠,它看起来像你可能会创建该文件一个地方,然后试图从不同的地方阅读:

// _preferencesPath ~= @"\\srvusadcweb01\wips_test\Cortland\fileName.xml": 
XmlReader.Create(_preferencesPath, settings); 

XmlWriter.Create(Path.Combine(Path.GetDirectoryName(@"\\server"), CONFIG_FILE_NAME)); 

除非@"\\server"只是一个编辑,以使事情有点更“通用”。

编辑

您是否尝试使用XmlWriter.Create()覆盖现有文件?如果是的话,你可能要检查了这一点:
C# Serialization to file, overwrite if exists

XmlWriter.Create(File.Create(fileName), settings) 
+0

是的服务器是名称的掩码 – user1901820

+0

在我的代码中,两者都是相同的,只是编辑,使其通用。 – user1901820

+0

@ user1901820什么是错误? – JDB