Edit XML Files using C#

XML is among the W3C recommended, structured formats, commonly used to store and transmit data. It is vastly required by developers to edit the stored XML data with the applications. To ease the requirement of editing, this article guides on how to edit the XML file data using C#.

.NET API to Edit XML Files

GroupDocs.Editor provides document editing solutions and APIs to edit a large list of different file formats. It is the .NET API that can be used along with external editors for visual editing. In this article, we will use GroupDocs.Editor for .NET for editing XML data within .NET application.

To download the DLLs or MSI installer, visit the downloads section or install the API in your .NET application via NuGet.

PM> Install-Package GroupDocs.Editor

How to Edit XML Files using C#

Coming straight to the objective, we will modify the XML data by replacing a value with another. The following are the steps to edit or update the XML file using C#.

  • Load the XML data file using Editor class.
  • Prepare the XML editing options using the XmlEditOptions class.
  • For editing, create the EditableDocument as source content using the Edit method and the prepared editing options.
  • From the EditableDocument, get the original content of the XML file using GetContent method.
  • Update values in the XML content.
  • Now create a new EditableDocument from the updated XML content using FromMarkup method.
  • For saving the updated content in different formats, prepare relevant saving options like WordProcessingSaveOptions or TextSaveOptions.
  • Save the updated XML data in any format using the Save method.

The following C# code snippet shows how to edit the XML file and update the data, later save it in any other format.

Get a Free License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To sum up, we have learned to programmatically edit XML file data using C#. You can further explore other features of GroupDocs.Editor using the documentation. To clarify any ambiguity, contact us on the forum.

See Also