我正在构建一个应用程序,它将使用文本框输入来编辑 xml 文件的内容。我已经了解了使用 C# 创建新的 xml 文件的基本概念,但是在尝试更新文件时仍然会出错。请帮忙?
(我是一个需要很多帮助的新开发人员。如果我需要发布其他任何内容,请添加评论。)
编辑: 这是我的问题所在:doc.CreateElement()
. 它不会更新文件。如果我单击保存按钮,文件本身不会更新。
这是我尝试过的:
文件名.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}" />