【问题标题】:Updating XML file with C# and WPF使用 C# 和 WPF 更新 XML 文件
【发布时间】:2012-12-13 18:15:44
【问题描述】:

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

(我是一个新开发者,需要很多帮助。如果我需要发布任何其他内容,请添加cmets。)

编辑: 这是我的问题所在: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}" />

【问题讨论】:

  • 您遇到了哪些错误?
  • 您不能将System.Xml.CreateElement() 的结果分配给System.Xml.Linq.XElement,它们是完全不同的类。 LoadXml() 需要一个 XML 字符串,“WIPSConfig”当然不是。您尝试创建一个XElement,然后将其丢弃而不做任何事情。然后你尝试反序列化一个 XmlReader,它不是一个可序列化的类。请告诉我们您想要实现的目标,而不是您想要做的事情,因为您想要做的事情毫无意义。

标签: c# wpf xml


【解决方案1】:

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

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

【讨论】:

  • 是的,服务器是名称的掩码
  • 在我的代码中,两者是相同的,只是经过编辑使其具有通用性。
  • 这是我的问题所在...... doc.CreateElement() 它不会更新文件。如果我单击保存按钮,文件本身不会更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 2012-01-02
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多