Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,
One quick question.
I need to do the following modification in a xml config file.
Need to replace the following:

<security mode="TransportCredentialOnly"><transport clientCredentialType="Windows"/>

with

<security mode="Transport">
<transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />

How to do this. Please help.

What I have tried:

I tried googling and done something like this...
XmlAttribute formId = (XmlAttribute)xmlDoc.SelectSingleNode("//security[@mode='TransportCredentialOnly'];
if (formId != null)
{
formId.Value = "Transport"; // Set to new value.
}
xmlDoc.Save("C:\\temp\\web.config");

But not getting a clue how to add the following attributes: proxyCredentialType, clientCredentialType, algorithmSuite
Posted
Updated 8-Aug-18 4:56am
v2

1 solution

Something like the following should work.
XmlNode node = doc.SelectSingleNode("//security");
XmlAttribute attr = doc.CreateAttribute("proxyCredential");
attr.Value = "attribute-value";
node.Attributes.Append(attr);
 
Share this answer
 
Comments
Dinesh Kumar Dora 9-Aug-18 1:54am    
Super Thanks Eric.
Dinesh Kumar Dora 9-Aug-18 5:44am    
Eric, i tried your method but getting the output like this:
<security mode="Transport" proxyCredential="None" realm="">
<transport clientCredentialType="Windows" />


I want it in this way:
<security mode="Transport">
<transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />

Please help. how can i get this.
Eric Lynch 9-Aug-18 7:46am    
In your question, you didn't mention that you wanted to change the "transport" node. To change the "transport" node, you want the following:

XmlNode node = doc.SelectSingleNode("//security/transport");

You should really take the time to learn XPATH. This is the syntax for the parameter to SelectSingleNode. Then, you can answer this question for yourself. I'll leave changing the message node as a learning exercise :)

For information on XPATH syntax, see the following:

https://www.w3schools.com/xml/xpath_syntax.asp

For information on SelectSingleNode, see the following:

https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectsinglenode

Also, if you'll be dealing with XML longer term, you may want to check out LINQ to XML. While the XmlDocument API continues to work fine, LINQ to XML offers many advantages. For more information on LINQ to XML, check out:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/basic-queries-linq-to-xml
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/getting-started-linq-to-xml

With LINQ, if you're unfamiliar, you can choose between query syntax and method syntax. Query syntax is shown in many of the examples. Method syntax is more like traditional C# syntax. Any query syntax can also be expressed as method syntax. The reverse is not true.

Personally, I prefer method syntax. Either is fine, its up to you which works better for you. Basically, method syntax is a series of extensions to the IEnumerable interface. The following article summarizes the available methods:

https://www.codeproject.com/Articles/1240219/LINQ-Part-Standard-Methods-Tools-in-the-Toolbox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900