Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: Read/Write XML data in C# [Thread Next] Re: Read/Write XML data in C#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/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
