0

I have an xml response string and I want to change a value inside and log it.

<xml>
<ns2:abcd>
<password>sample</password>
</ns2:abcd>

I want to change the password value into encrypted version.

I am have tried using XmlDocument.SelectSingleNode but was thinking is there any better way than this?

alice7
  • 3,834
  • 14
  • 64
  • 93
  • start here http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx, probably with `XElement.Parse` – Jodrell Jan 23 '13 at 17:15

3 Answers3

3

Btw you need ns2 namespace to be declared, otherwise your xml will not be valid. After adding namespace definition, you can parse and modify your xml with Linq to Xml:

XDocument xdoc = XDocument.Parse(xml);
var passwordElement = xdoc.XPathSelectElement("//password");
passwordElement.Value = Encrypt((string)passwordElement);
xdoc.Save(path_to_xml);
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
1

No - there is no better way than using proper XML classes.

XmlDocument or XDocument would be perfectly fine for this task. If you XML is very large you may want to look into streaming with XmlReader (unlikely necessary in your case).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    Prefer `XDocument` to `XmlDocument` http://stackoverflow.com/questions/1542073/xdocument-or-xmldocument – Jodrell Jan 23 '13 at 17:27
0

You might also consider looking into xsd.exe. With xsd.exe, you can deserialize your xml into a type-safe object model. From there, it's easy to manipulate the data.

Alex Dresko
  • 5,179
  • 3
  • 37
  • 57