Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active Nov 6, 2021
Embed
What would you like to do?
Edit XML file and Update Values using C#
// 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