Related blog post: How to Edit XML files using C#
Last active
November 6, 2021 03:48
-
-
Save GroupDocsGists/d0ba38fe72fbf40caba648534764da59 to your computer and use it in GitHub Desktop.
Edit XML file and Update Values using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Edit XML file by updating values using C# | |
using (Editor editor = new Editor("path/data.xml")) | |
{ | |
// Create XML editing options | |
Options.XmlEditOptions editOptions = new XmlEditOptions(); | |
editOptions.AttributeValuesQuoteType = QuoteType.DoubleQuote; | |
editOptions.RecognizeEmails = true; | |
editOptions.RecognizeUris = true; | |
editOptions.TrimTrailingWhitespaces = true; | |
// EditableDocument Settings | |
using (EditableDocument beforeEdit = editor.Edit(editOptions)) | |
{ | |
// Edit whatever | |
string originalTextContent = beforeEdit.GetContent(); | |
string updatedTextContent = originalTextContent.Replace("John", "Samuel"); | |
List<IHtmlResource> allResources = beforeEdit.AllResources; | |
// Create EditableDocument with updated content | |
using (EditableDocument afterEdit = EditableDocument.FromMarkup(updatedTextContent, allResources)) | |
{ | |
// Create WordProcessing save options | |
Options.WordProcessingSaveOptions wordSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docx); | |
// Create TXT save options | |
Options.TextSaveOptions txtSaveOptions = new TextSaveOptions(); | |
txtSaveOptions.Encoding = System.Text.Encoding.UTF8; | |
// Save edited XML data in DOCX and TXT format | |
editor.Save(afterEdit, "path/xmlData.docx", wordSaveOptions); | |
editor.Save(afterEdit, "path/xmlData.txt", txtSaveOptions); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment