I am trying to update an XML, but would like to retain the formatting{indenting} and not insert the XmlDeclaration tag. The following the snippet that I am applying.
let UpdateAndSave (configFileName:string) =
let xmlDoc = XDocument.Load(configFileName)
xmlDoc.XPathEvaluate(@"Product/SomeTag/Attr[@type='Value']") :?> _
|> Seq.cast<XElement>
|> Seq.iter (fun elem -> elem.AddAfterSelf(XElement.Parse("<Risk type=\"123\" methodology=\"Sample\"/>")))
do
let xmlWriterSettings = new XmlWriterSettings()
xmlWriterSettings.OmitXmlDeclaration = true
xmlWriterSettings.Indent = true
using(XmlWriter.Create(configFileName, xmlWriterSettings))(fun xmlFileWriter ->
xmlDoc.Save(xmlFileWriter)
)
configFileName
After the update, the xml formatting is totally lost and also the ‘XmlDeclaration’ tag gets inserted. Am I missing something?
In F#,
=is comparison operator while<-is an assignment to a mutable field. I expect the compiler to complain thatxmlWriterSettings.OmitXmlDeclaration = trueis notunitvalue in your example.Moreover, your
doblock seems redundant andusingcould be replaced byusefor conciseness.