473,396 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XML adding a node in C#

Hi,

I have found a very useful piece of code in the msdn which shows how to load
an xml config file, search for a particular value and replace it.
Could someone possibly convert it to C# from the VB I have?
I also would like to use similar code to update the machine.config file so
if someone could explain how to add a node ("appSettings") if node not found
and also how to add a key value pair into the "appSettings" node if found I
would be extremely grateful.

The code snippet I have from msdn is as follows

' Loads the config file into the XML DOM.
Dim XmlDocument As New System.Xml.XmlDocument()
XmlDocument.Load(FileInfo.FullName)

' Finds the right node and change it to the new value.
Dim Node As System.Xml.XmlNode
Dim FoundIt As Boolean = False
For Each Node In XmlDocument.Item("configuration").Item("appSetting s")
If Node.Name = "add" Then ' skip any comments
If Node.Attributes.GetNamedItem("key").Value = "ServerName" Then
Node.Attributes.GetNamedItem("value").Value = ProvidedName
FoundIt = True
End If
End If
Next Node

Apologies if this is the wrong group, I am happy to repost to a more correct
one if someone can point me the way.

--
KM

Nov 12 '05 #1
2 7654
There's a much easier way.

// Load the machine.config file into the XML document
Uri baseUri = new Uri(typeof(string).Assembly.Location);
Uri uri = new Uri(baseUri, "config\\machine.config");
XmlDocument doc = new XmlDocument();
doc.Load(uri.AbsolutePath);
Then use XPath to find what you want:

// Finds the right node and change it to the new value.
XmlNode node =
doc.SelectSingleNode("configuration/appSettings/add[@key = 'ServerName']");
if (node != null) {
Console.WriteLine(node.InnerText);
}

"Keith M" <ke***********@nospam.amsjv.com> wrote in message
news:42**********@glkas0286.greenlnk.net...
Hi,

I have found a very useful piece of code in the msdn which shows how to
load
an xml config file, search for a particular value and replace it.
Could someone possibly convert it to C# from the VB I have?
I also would like to use similar code to update the machine.config file so
if someone could explain how to add a node ("appSettings") if node not
found
and also how to add a key value pair into the "appSettings" node if found
I
would be extremely grateful.

The code snippet I have from msdn is as follows

' Loads the config file into the XML DOM.
Dim XmlDocument As New System.Xml.XmlDocument()
XmlDocument.Load(FileInfo.FullName)

' Finds the right node and change it to the new value.
Dim Node As System.Xml.XmlNode
Dim FoundIt As Boolean = False
For Each Node In XmlDocument.Item("configuration").Item("appSetting s")
If Node.Name = "add" Then ' skip any comments
If Node.Attributes.GetNamedItem("key").Value = "ServerName" Then
Node.Attributes.GetNamedItem("value").Value = ProvidedName
FoundIt = True
End If
End If
Next Node

Apologies if this is the wrong group, I am happy to repost to a more
correct
one if someone can point me the way.

--
KM


Nov 12 '05 #2
Ok, I've been having a play with this stuff and got something working.

Thanks

"Chris Lovett" <so*****@nospam.please> wrote in message
news:Os********************@comcast.com...
There's a much easier way.

// Load the machine.config file into the XML document
Uri baseUri = new Uri(typeof(string).Assembly.Location);
Uri uri = new Uri(baseUri, "config\\machine.config");
XmlDocument doc = new XmlDocument();
doc.Load(uri.AbsolutePath);
Then use XPath to find what you want:

// Finds the right node and change it to the new value.
XmlNode node =
doc.SelectSingleNode("configuration/appSettings/add[@key = 'ServerName']"); if (node != null) {
Console.WriteLine(node.InnerText);
}

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the...
9
by: James Geurts | last post by:
Hey all... I posted this in the vs.net ide group too, but people are not answering, so I figured that it might be more appropriate here. I'm not sure if I'm adding a designer to my code properly. ...
3
by: Saradhi | last post by:
Hi All, Here I am facing a performance problem with the TreeView Node renaming. I am displaying a hierarchy Data in a treeview in my Windows C# Application. My tree view represents an...
3
by: Brian Henry | last post by:
If i already have a tree view created, and want to add another new node to it, how would i do so? Is there a way to throught tags or anything? like i have this RootNode | +-- Child 1 +--...
1
by: Joel Byrd | last post by:
I am having a problem adding a node to the page. This works on other browser (Opera, Firefox, etc.), but not in IE. The following is the function to add the node: (the id "test_div" is a...
1
by: John Wilhelm | last post by:
I'm having a problem in by VB.net 2005 application. When i try to get a node from my app.config file the node come back with "nothing". The xmldocment loads OK, but I can't retrive a node. The...
4
by: dabbakal | last post by:
Hello, am a new member and this is my first posting. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have...
0
gundarap
by: gundarap | last post by:
Hello everybody, I'm a starter to Python programming. My goal is to first read an xml file and add the elements to the dictionary. Note that xml file I'm using is a simple file without any...
6
by: SQACSharp | last post by:
I'm using the EnumChildWindows API with an EnumChildWndProc callback to populate the treeview. The output will be something similar to spy+ + How can I specify the parent when adding a new node...
1
by: Bart Steur | last post by:
Hi As a VB6 user I'm unfamiliar with the Treeview control of VB2005. I read it was changed but I have a hard time finding the right help/samples for my problem. In VB6 you had 1 collection,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.