Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Read/Write XML data in C#

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 10/10/2008 4:10:00 PM

RLEE71 wrote:

> Now how would I handle setting the passwords on each of the three users? 

As already suggested, you can use data binding with an XmlDataProvider 
and your XML document as the Source. That way you can use XPath 
expressions to bind nodes in the XML document to controls in the WPF 
user interface.
Here is an example that assumes your XML document as the Source of the 
XmlDataProvider:

<Window x:Class="WpfApplication18.Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="Window1" Height="300" Width="300">
     <Window.Resources>
         <XmlDataProvider x:Key="xdp1" Source="XMLFile1.xml" XPath="RIBCL"/>
     </Window.Resources>
     <StackPanel DataContext="{Binding Source={StaticResource xdp1}}">
         <TextBlock Text="MOD NETWORK SETTINGS" FontSize="14" />
         <StackPanel Orientation="Horizontal">
             <Label Content="IP ADRESS: "/>
             <TextBox Text="{Binding 
XPath=LOGIN/RIB_INFO/MOD_NETWORK_SETTINGS/IP_ADDRESS/@VALUE}" 
VerticalContentAlignment="Center" MinWidth="80" TextAlignment="Right" />
         </StackPanel>
         <StackPanel Orientation="Horizontal">
             <Label Content="SUBNET MASK: "/>
             <TextBox Text="{Binding 
XPath=LOGIN/RIB_INFO/MOD_NETWORK_SETTINGS/SUBNET_MASK/@VALUE}" 
VerticalContentAlignment="Center" MinWidth="80" TextAlignment="Right" />
         </StackPanel>
         <StackPanel Orientation="Horizontal">
             <Label Content="GATEWAY IP ADDRESS: "/>
             <TextBox Text="{Binding 
XPath=LOGIN/RIB_INFO/MOD_NETWORK_SETTINGS/GATEWAY_IP_ADDRESS/@VALUE}" 
VerticalContentAlignment="Center" MinWidth="80" TextAlignment="Right" />
         </StackPanel>
         <StackPanel Orientation="Horizontal">
             <Label Content="DNS NAME: "/>
             <TextBox Text="{Binding 
XPath=LOGIN/RIB_INFO/MOD_NETWORK_SETTINGS/DNS_NAME/@VALUE}" 
VerticalContentAlignment="Center" MinWidth="80" TextAlignment="Right" />
         </StackPanel>
         <Separator></Separator>
         <TextBlock FontSize="14" Text="USER INFO"/>
         <ListView ItemsSource="{Binding XPath=LOGIN/USER_INFO/ADD_USER}">
             <ListView.ItemTemplate>
                 <DataTemplate>
                     <StackPanel Orientation="Horizontal">
                         <Label Content="{Binding 
XPath=@USER_NAME}"></Label>
                         <TextBox Text="{Binding XPath=@PASSWORD}" 
MinWidth="80" VerticalContentAlignment="Center"></TextBox>
                     </StackPanel>
                 </DataTemplate>
             </ListView.ItemTemplate>
         </ListView>
         <Separator></Separator>
         <Button Name="Save" Click="Save_Click">Save</Button>
     </StackPanel>
</Window>


The Save button could then save the changes when needed, in my sample 
below it simply displays the changed XML:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace WpfApplication18
{
     /// <summary>
     /// Interaction logic for Window1.xaml
     /// </summary>
     public partial class Window1 : Window
     {
         public Window1()
         {
             InitializeComponent();
         }

         private void Save_Click(object sender, RoutedEventArgs e)
         {
             XmlDocument doc = 
((XmlDataProvider)FindResource("xdp1")).Document;
             //doc.Save("file.xml")
             using (StringWriter sw = new StringWriter())
             {
                 doc.Save(sw);
                 MessageBox.Show(string.Format("Actual XML\r\n{0}", 
sw.ToString()));
             }
         }
     }
}
-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent