Need help updating xml file with C# and WPF

CoppyZenDep

New Member
I am building an application that will use textbox input to edit the contents of a xml file. I have gotten the basic concepts of creating NEW xml files with C#, but I continue to get errors when trying to update a file. PLEASE.. help?(I am a new developer who needs a lot of help. Please add comments if I need to post anything else.)This is what I've tried:fileName.xml:\[code\] <ApplicationPreferences> <Facility>2400</Facility> <FacilityName>Somewhere</FacilityName> </ApplicationPreferences>\[/code\]Code:\[code\]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(); } }\[/code\]XAML for textboxes:\[code\] <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}" />\[/code\]
 
Back
Top