简体   繁体   中英

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?

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.

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");
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM