Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active November 6, 2021 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/d0ba38fe72fbf40caba648534764da59 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/d0ba38fe72fbf40caba648534764da59 to your computer and use it in GitHub Desktop.
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