Manual:XML Import file manipulation in CSharp

Overview
This page shows how to use the MediaWiki schema with Visual Studio .NET C# to manipulate a MediaWiki XML import>Special:MyLanguage/Manual:Importing XML dumps|import file in code using object-oriented programming instead of working directly with raw XML.

One use case for this is that you might have a number of pages in a wiki site that need to be modified. One way to do this is to export them to an XML file, then manipulate the XML file, and then import the XML file back. Of course, you should be sure that users cannot modify these files during the span between export and re-import. For sites with moderate usage, this approach might be appropriate.

Schema
As shown in this abbreviated example of an XML import file below, the schemaLocation of the XML file is at https://www.mediawiki.org/xml/export-0.3.xsd:

First, download the MediaWiki schema at https://www.mediawiki.org/xml/export-0.3.xsd. Place the schema file in a .NET project folder, and consider renaming the file to something more intuitive such as MediaWikiExport.xsd. Using Visual Studio.NET's xsd.exe tool, you can generate a .NET class file based on this schema using this VS.NET command line prompt:

 xsd c:/inetpub/wwwroot/MyProject/MediaWikiExport.xsd /c

This command will create a class file named MediaWikiExport.cs.

Class Diagram
The auto-generated Class file will look like this:

Schema Diagram
The schema will look like this:

.NET Project
After you add your new auto-generated class file, add the file into your .NET project, such as a console application project.

In this code sample, you will see examples of how to work with the XML file in an object-oriented way instead of parsing the raw XML. Note that this code sample below was used for the 1.13.2 version of MediaWiki.

C# 3.0 version
Here's the same example using C# 3.0 features, including type inference and a lambda expression.