Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Read/Write XML data in C# [Thread Next] Re: Read/Write XML data in C#To: NULL Date: 1/31/2011 11:21:00 AM <a href="http://www.mindstick.com/Articles/d10f55c9-539d-4f63-bc80-0314f4ca029f/?How to read and write XML in C#">read and write xml in c#</a> be used to dataset for reading xml data more info check this link
<a href="http://www.mindstick.com/Articles/d10f55c9-539d-4f63-bc80-0314f4ca029f/?How to read and write XML in C#">http://www.mindstick.com/Articles/d10f55c9-539d-4f63-bc80-0314f4ca029f/?How to read and write XML in C#</a>
> On Thursday, October 09, 2008 12:22 PM RLEE7 wrote:
> I'm using VS C# 2008 Express. I want to read data from an XML file and
> display into a text box. I then want to be able to edit the data in the text
> box and save back to the XML file. I've seen various examples of how to do
> this, but I think that my particular XML file is more complicated. Any help
> would be greatly appreciated. I'm concerned with the data in the IP_ADRESS,
> SUBNET_MASK, GATEWAY_IP_ADRESS, and DNS_NAME tags under MOD_NETWORK_SETTINGS.
> Also want to edit the ADD_USER PASSWORD data.
>
> Here is the XML file:
>
> <?xml version="1.0" encoding="utf-8"?>
> <!-- HPONCFG VERSION = "1.5.1.1" -->
> <!-- Generated 09/09/08 11:45:12 -->
> <RIBCL VERSION="2.1">
> <LOGIN USER_LOGIN="Administrator" PASSWORD="password">
> <DIR_INFO MODE="write">
> <MOD_DIR_CONFIG>
> <DIR_AUTHENTICATION_ENABLED VALUE="N" />
> <DIR_LOCAL_USER_ACCT VALUE="Y" />
> <DIR_SERVER_ADDRESS VALUE="" />
> <DIR_SERVER_PORT VALUE="636" />
> <DIR_OBJECT_DN VALUE="" />
> <DIR_OBJECT_PASSWORD VALUE="" />
> <DIR_USER_CONTEXT_1 VALUE="" />
> <DIR_USER_CONTEXT_2 VALUE="" />
> <DIR_USER_CONTEXT_3 VALUE="" />
> </MOD_DIR_CONFIG>
> </DIR_INFO>
> <RIB_INFO MODE="write">
> <MOD_GLOBAL_SETTINGS>
> <RBSU_POST_IP VALUE="Y" />
> </MOD_GLOBAL_SETTINGS>
> <MOD_NETWORK_SETTINGS>
> <SPEED_AUTOSELECT VALUE="N" />
> <NIC_SPEED VALUE="100" />
> <FULL_DUPLEX VALUE="Y" />
> <DHCP_ENABLE VALUE="N" />
> <DHCP_GATEWAY VALUE="N" />
> <DHCP_DNS_SERVER VALUE="N" />
> <DHCP_STATIC_ROUTE VALUE="N" />
> <DHCP_WINS_SERVER VALUE="N" />
> <REG_WINS_SERVER VALUE="Y" />
> <IP_ADDRESS VALUE="20.5.236.19" />
> <SUBNET_MASK VALUE="255.255.255.0" />
> <GATEWAY_IP_ADDRESS VALUE="20.5.236.254" />
> <DNS_NAME VALUE="name.com" />
> <DOMAIN_NAME VALUE="" />
> <PRIM_DNS_SERVER value="0.0.0.0" />
> <SEC_DNS_SERVER value="0.0.0.0" />
> <TER_DNS_SERVER value="0.0.0.0" />
> <PRIM_WINS_SERVER value="0.0.0.0" />
> <SEC_WINS_SERVER value="0.0.0.0" />
> <STATIC_ROUTE_1 DEST="0.0.0.0" GATEWAY="0.0.0.0" />
> <STATIC_ROUTE_2 DEST="0.0.0.0" GATEWAY="0.0.0.0" />
> <STATIC_ROUTE_3 DEST="0.0.0.0" GATEWAY="0.0.0.0" />
> </MOD_NETWORK_SETTINGS>
> <LICENSE>
> <ACTIVATE KEY="332DX8V87SZYLMMGYXGT4GD6M" />
> </LICENSE>
> </RIB_INFO>
> <USER_INFO MODE="write">
> <ADD_USER USER_NAME="ilo03" USER_LOGIN="ilo03" PASSWORD="paswword">
> <ADMIN_PRIV value="Y" />
> <REMOTE_CONS_PRIV value="Y" />
> <RESET_SERVER_PRIV value="Y" />
> <VIRTUAL_MEDIA_PRIV value="Y" />
> <CONFIG_ILO_PRIV value="Y" />
> </ADD_USER>
> <ADD_USER USER_NAME="ilo02" USER_LOGIN="ilo02" PASSWORD="password1">
> <ADMIN_PRIV value="N" />
> <REMOTE_CONS_PRIV value="Y" />
> <RESET_SERVER_PRIV value="Y" />
> <VIRTUAL_MEDIA_PRIV value="Y" />
> <CONFIG_ILO_PRIV value="N" />
> </ADD_USER>
> <ADD_USER USER_NAME="ilo01" USER_LOGIN="ilo01" PASSWORD="password2">
> <ADMIN_PRIV value="N" />
> <REMOTE_CONS_PRIV value="N" />
> <RESET_SERVER_PRIV value="Y" />
> <VIRTUAL_MEDIA_PRIV value="Y" />
> <CONFIG_ILO_PRIV value="N" />
> </ADD_USER>
> </USER_INFO>
> </LOGIN>
> </RIBCL>
>> On Thursday, October 09, 2008 12:41 PM Martin Honnen wrote:
>> RLEE71 wrote:
>>
>> With VS 2008 you can and should use LINQ to XML to manipulate your XML:
>> http://msdn.microsoft.com/en-us/library/bb387098.aspx
>>
>> Here is sample code that loads your XML sample, finds the
>> MOD_NETWORK_SETTINGS element and changes the VALUE attribute of the
>> DNS_NAME child element:
>>
>> XDocument doc = XDocument.Load(@"doc.xml");
>> XElement mns =
>> doc.Descendants("MOD_NETWORK_SETTINGS").FirstOrDefault();
>> if (mns != null)
>> {
>> XElement dname = mns.Element("DNS_NAME");
>> dname.SetAttributeValue("VALUE", "foo");
>> // make more changes here
>> // then save
>> doc.Save("doc.xml");
>> }
>>
>> --
>>
>> Martin Honnen --- MVP XML
>> http://JavaScript.FAQTs.com/
>>> On Thursday, October 09, 2008 1:16 PM Martin Honnen wrote:
>>> RLEE71 wrote:
>>>
>>> If you want to write a WPF application you could also consider to use
>>> data binding to bind your controls to an XmlDataProvider.
>>>
>>> --
>>>
>>> Martin Honnen --- MVP XML
>>> http://JavaScript.FAQTs.com/
>>>> On Thursday, October 09, 2008 2:46 PM RLEE7 wrote:
>>>> Thanks Martin. That code worked. Now how would I pull the current value into
>>>> a text box, and then change it and write it back from the data entered in the
>>>> textbox?
>>>>
>>>> For instance, I havae a form that has a TextBox for:
>>>> IP Address
>>>> Subnet Mask
>>>> Gateway
>>>> DNS Name
>>>>
>>>> The XML file would have default values that I would like populated in each
>>>> textbox. Then if a change was required, one would just change the data in the
>>>> textbox and then save the data back to the xml file.
>>>>
>>>> I'm a bit new to programming, that's why I like Visual Studio, so forgive me
>>>> if these questions seem a bit rudimentary.
>>>>
>>>> Ron
>>>>
>>>> "RLEE71" wrote:
>>>>> On Thursday, October 09, 2008 3:18 PM RLEE7 wrote:
>>>>> OK, I figured out how to change the data from a textbox entry.
>>>>> I changed your code:
>>>>> dname.SetAttributeValue("VALUE", "foo"); to:
>>>>> dname.SetAttributeValue("VALUE", textBox4.Text);
>>>>>
>>>>> I imageine that I'll need an XElement statement for each attribute that I
>>>>> want to change, e.g., XElement ipadd, XElement submask, XElement gateway, etc.
>>>>>
>>>>> Ron
>>>>> "Martin Honnen" wrote:
>>>>>> On Thursday, October 09, 2008 4:13 PM RLEE7 wrote:
>>>>>> OK, this worked well.
>>>>>>
>>>>>> private void button4_Click(object sender, EventArgs e)
>>>>>> {
>>>>>> XDocument doc = XDocument.Load(@"c:\iloconf.xml");
>>>>>> XElement mns =
>>>>>> doc.Descendants("MOD_NETWORK_SETTINGS").FirstOrDefault();
>>>>>> if (mns != null)
>>>>>> {
>>>>>> XElement ipadd = mns.Element("IP_ADDRESS");
>>>>>> ipadd.SetAttributeValue("VALUE", textBox1.Text);
>>>>>>
>>>>>> XElement subnet = mns.Element("SUBNET_MASK");
>>>>>> subnet.SetAttributeValue("VALUE", textBox2.Text);
>>>>>>
>>>>>> XElement gateway = mns.Element("GATEWAY_IP_ADDRESS");
>>>>>> gateway.SetAttributeValue("VALUE", textBox3.Text);
>>>>>>
>>>>>> XElement dname = mns.Element("DNS_NAME");
>>>>>> dname.SetAttributeValue("VALUE", textBox4.Text);
>>>>>> // make more changes here
>>>>>> // then save
>>>>>> doc.Save(@"c:\iloconf.xml");
>>>>>> }
>>>>>> }
>>>>>>
>>>>>> Now how would I handle setting the passwords on each of the three users? I
>>>>>> wouldn't be able to use the statement:
>>>>>> XElement ui = doc.Descendants("USER_INFO").FirstOrDefault();
>>>>>>
>>>>>> I would have to identify which ADD_USER under the USER_INFO element.
>>>>>>
>>>>>> Thanks,
>>>>>> Ron
>>>>>>
>>>>>> "Martin Honnen" wrote:
>>>>>>> On Friday, October 10, 2008 10:09 AM Martin Honnen wrote:
>>>>>>> RLEE71 wrote:
>>>>>>>
>>>>>>>
>>>>>>> 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/
>>>>>>> Submitted via EggHeadCafe
>>>>>>> Serializing Excel data for input to any Google visualization
>>>>>>> http://www.eggheadcafe.com/tutorials/aspnet/571d84dc-9fcf-44de-b2ad-005c12372ab3/serializing-excel-data-for-input-to-any-google-visualization.aspx
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
