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# Re: Read/Write XML data in C#To: NULL Date: 10/9/2008 1:13:00 PM
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:
> RLEE71 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.
>
> 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/
>
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
