Re: XML adding a node in C#

Tech-Archive recommends: Speed Up your PC by fixing your registry



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" <keith.mallett@xxxxxxxxxxxxxxxx> wrote in message
news:42a55b9d$1_1@xxxxxxxxxxxxxxxxxxxxxxxxx
> 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("appSettings")
> 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
>
>
>
>
>


.



Relevant Pages

  • XML adding a node in C#
    ... 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. ... ' Loads the config file into the XML DOM. ...
    (microsoft.public.dotnet.xml)
  • Re: LinkButton Event Handling (Dynamic controls)
    ... which is run on the page load and every grid ... postback. ... on page init not on page load. ... Dim loCurrentPage As New Literal ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Datagrid question
    ... Are you sure that you are using a Datagrid, you are talking in your text about a DataGridViewRow, you cannot use the latter in a DataGrid. ... I load the grid and display the results. ... Dim dtTempData As New DataTable ... Private Sub cmdFindCrushingRun_ClickHandles cmdFindCrushingRun.Click ...
    (microsoft.public.dotnet.languages.vb)
  • Datagrid question
    ... I load the grid and display ... Dim dtTempData As New DataTable ... 'load the data on the tab page by calling the appropriate load data function ... Private Sub cmdFindCrushingRun_Click(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Handle of running application
    ... The transit web site will not load the reply page, it always crashes. ... I will endeavor to get familiar with HttpWebRequest and HttpWebResponse. ... In the top right corner of this page you will see "Today's Bus Stop Schedule". ... Dim bsRoute As String ...
    (microsoft.public.dotnet.framework)