473,796 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ("appSetting s") if node not found
and also how to add a key value pair into the "appSetting s" 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.XmlD ocument()
XmlDocument.Loa d(FileInfo.Full Name)

' Finds the right node and change it to the new value.
Dim Node As System.Xml.XmlN ode
Dim FoundIt As Boolean = False
For Each Node In XmlDocument.Ite m("configuratio n").Item("appSe ttings")
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 7690
There's a much easier way.

// Load the machine.config file into the XML document
Uri baseUri = new Uri(typeof(stri ng).Assembly.Lo cation);
Uri uri = new Uri(baseUri, "config\\machin e.config");
XmlDocument doc = new XmlDocument();
doc.Load(uri.Ab solutePath);
Then use XPath to find what you want:

// Finds the right node and change it to the new value.
XmlNode node =
doc.SelectSingl eNode("configur ation/appSettings/add[@key = 'ServerName']");
if (node != null) {
Console.WriteLi ne(node.InnerTe xt);
}

"Keith M" <ke***********@ nospam.amsjv.co m> wrote in message
news:42******** **@glkas0286.gr eenlnk.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 ("appSetting s") if node not
found
and also how to add a key value pair into the "appSetting s" 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.XmlD ocument()
XmlDocument.Loa d(FileInfo.Full Name)

' Finds the right node and change it to the new value.
Dim Node As System.Xml.XmlN ode
Dim FoundIt As Boolean = False
For Each Node In XmlDocument.Ite m("configuratio n").Item("appSe ttings")
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******** ************@co mcast.com...
There's a much easier way.

// Load the machine.config file into the XML document
Uri baseUri = new Uri(typeof(stri ng).Assembly.Lo cation);
Uri uri = new Uri(baseUri, "config\\machin e.config");
XmlDocument doc = new XmlDocument();
doc.Load(uri.Ab solutePath);
Then use XPath to find what you want:

// Finds the right node and change it to the new value.
XmlNode node =
doc.SelectSingl eNode("configur ation/appSettings/add[@key = 'ServerName']"); if (node != null) {
Console.WriteLi ne(node.InnerTe xt);
}

Nov 12 '05 #3

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

Similar topics

10
5497
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 following: struct Node { int integer_value;
9
5489
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. I'm only adding it to the tree node, where each custom tree node is then added to a normal tree control I'm trying to get an add-in to interact with the property window in VS.Net. Basically, I created a tool window containing a custom tree...
3
6060
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 hierarchical view of Parent Nodes and projects where in a projectnode can be added to any ParentNode and hence we may have a project node added to 100 Parent nodes. In this one, I have an operation of Renaming a Project Node. So whenever I am doing the...
3
2488
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 +-- Child 2 and i want to add a child node to child 1 how would i refrence it and add a
1
1559
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 defined style) Now, the interesting thing is that it works if I use the function in a link using "onClick='add_div()'" or "href='javascript: add_div();", but
1
2899
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 app.config file is listed below and the code is listed below that, the line with "-->>" is where i try to get the node. Can anyone see where I'm going wrong. A dump of the xmlDocument is listed below the code. Note: "MyKey" is set to "Reports"
4
2849
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 written a program in c++ in linked list but am facing two problems. (1) when i delete an item that is not stored the program hangs and (2) I cannot modify the program to insert the node at the back i can only do it to insert at the front. here is the...
0
903
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 attributes... e.x. <?xml?> <AR> <BD>Data</BD> </AR> Next step is to add a node to the xml file and verify if the node exists in the xml file or not.
6
2518
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 ?? When adding a new node is there any way to get an handle or something else to be able add the childs to the correct parent ? Thanks!
1
1906
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, and adding a node was quite simple. Just specify the nodes key and position relative to the key where you want to add your new node. In VB2005 you can't do that anymore. I looking for a sample or help how this
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.