Changing a value within an XML document using F#

I needed to simulate some user interaction with our webservices in an automated manner. This post is specific to one small part of this requirement – here we’re going to simply take some XML and change a value within the data and return this altered data.

The code is very simple but demonstrates how to deal with namespaces as well as using XmlDocument and XPath.

// takes a string representing the XML and allows us to find a node and change the inner text, also shows
// how to set-up namespaces (note: For simplicity there's No error handling in this sample)
let changeValue xmlData =
let nameTable = NameTable()
let namespaceManager = XmlNamespaceManager(nameTable)
namespaceManager.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope")
let xml = XmlDocument()
xml.LoadXml xmlData
let current = xml.SelectSingleNode("//soap:Envelope/soap:Body/someElement", namespaceManager)
current.InnerText <- "NEW TEXT"
xml.OuterXml

Note: I’m using XmlDocument here and XPath for simplicity but obviously this is not the most performant on large documents.