user829174
user829174

Reputation: 6362

Editing xml file with c#

I have the following xml file

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfParams xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Params Key="Domain" Value="User123">
    <Variable>
      <Name>Domain</Name>
      <Type>String</Type>
      <Value>User123</Value>
    </Variable>
  </Params>
  <Params Key="Password" Value="Password123">
    <Variable>
      <Name>Password</Name>
      <Type>String</Type>
      <Value>Password123</Value>
    </Variable>
  </Params>
  <Params Key="Username" Value="Domain123">
    <Variable>
      <Name>Username</Name>
      <Type>String</Type>
      <Value>Domain123</Value>
    </Variable>
  </Params>
</ArrayOfParams>

I would like to change the password from Password123 to NewPassword123 The xml should be changed in 2 places:

<Params Key="Password" Value="Password123">

and

<Value>Password123</Value>

How can this be done?

EDIT the XML already exist, not my design. i just need to change it

I tried to use XDocument, but am having problems with the query. can you provide a linq that knows how to query it?

Upvotes: 0

Views: 4806

Answers (3)

Casperah
Casperah

Reputation: 4564

It is quite easy with System.Xml.XmlDocument:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(source);
XmlNode password = xmlDocument.SelectSingleNode("//Params[@Key='Password']");
password.Attributes["Value"] = "NewPassword123";
XmlNode value = password.SelectSingleNode("./Value");
value.InnerXml = "NewPassword123";
string source = xmlDocument.OuterXml;
xmlDocument.Save(destPath);

Hope this helps you.

Upvotes: 0

marc_s
marc_s

Reputation: 755541

This would be one way to do it - using the "older" style XmlDocument class which loads the entire XML into memory and allows you to "navigate" around its structure. Works fine if your XML isn't too big (like here).

// create XML document and load contents from file
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"D:\temp\test.xml");   // adapt this to your own path!

// get the <Params> node with the Key=Password attribute
XmlNode paramsNode = xdoc.SelectSingleNode("/ArrayOfParams/Params[@Key='Password']");

if (paramsNode != null)
{
    // set the value of the "Value" attribute to the new password
    paramsNode.Attributes["Value"].Value = "NewPassword123";

    // get the "Variable/Value" subnode under that <Params> node
    XmlNode valueSubnode = paramsNode.SelectSingleNode("Variable/Value");

    if (valueSubnode != null)
    {
       valueSubnode.InnerText = "NewPassword123";

       // save XmlDocument back out to a new file name
       xdoc.Save(@"D:\temp\modified.xml");
    }
}

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

How about using LINQ to XML?

var doc = XDocument.Parse(xmlString);

var passwordParams = doc.Root.Elements("Params").SingleOrDefault(e => (string)e.Attribute("Key") == "Password");

if(passwordParams != null)
{
    passwordParams.Attribute("Value").Value = newPasswordString;
    passwordParams.Element("Variable").Element("Value").Value = ewPasswordString;
}

After that you can save the document wherever you want.

I can't test it right now, but general idea should be clear.

Upvotes: 1

Related Questions