gpt4 book ai didi

c# - 使用 C# 和 WPF 更新 XML 文件

转载 作者:数据小太阳 更新时间:2023-10-29 02:45:50 26 4
gpt4 key购买 nike

我正在构建一个应用程序,它将使用文本框输入来编辑 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}" />

最佳答案

马上,看起来您可能在一个地方创建文件,然后尝试从另一个地方读取它:

// _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)

关于c# - 使用 C# 和 WPF 更新 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13865943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com