Jump to content
MediaWiki

Manual:XML Import file manipulation in CSharp

From mediawiki.org

Overview

[edit ]

This page shows how to use the MediaWiki schema with Visual Studio .NET C# to manipulate a MediaWiki XML 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

[edit ]

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:

<mediawikixmlns="https://www.mediawiki.org/xml/export-0.3/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.mediawiki.org/xml/export-0.3/ https://www.mediawiki.org/xml/export-0.3.xsd"
version="0.3"
xml:lang="en">
<siteinfo>...</siteinfo>
<page>...</page>
<page>...</page>
<page>...</page>
</mediawiki>

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

[edit ]

The auto-generated Class file will look like this:

Auto-generated VS.NET C# class file based on the MediaWiki import schema


Schema Diagram

[edit ]

The schema will look like this:

MediaWiki import file schema


.NET Project

[edit ]

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.

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Xml;
usingSystem.Xml.Serialization;
namespaceWikiFileManipulation
{
classProgram
{
staticvoidMain(string[]args)
{
// name of the exported wiki file
stringfile="ExportedWikiPages.xml";
// instantiate MediaWikiType object
MediaWikiTypemw=newMediaWikiType();
// Open XML file containing exported wiki pages
System.Xml.XmlDataDocumentxml=newSystem.Xml.XmlDataDocument();
xml.Load(file);
// Deserialize the XML file into the MediaWikiType object
XmlSerializerserializer=newXmlSerializer(typeof(MediaWikiType));
System.Xml.XmlNodeReaderoReader=newSystem.Xml.XmlNodeReader(xml);
mw=(MediaWikiType)serializer.Deserialize(oReader);
// Loop through all the Pages in the MediaWikiType object
foreach(PageTypepinmw.page)
{
foreach(objectoinp.Items)
{
// Examine the RevisionType
if(oisRevisionType)
{
// Cast to RevisionType object
RevisionTyper=oasRevisionType;
// if you increment "timestamp" by one minute, then you'll be able to re-import file
r.timestamp=r.timestamp.AddMinutes(1);
// Update the value of the "text" of the revision
// this is the page text
TextTypetext=r.textasTextType;
text.Value=text.Value.Replace("oldvalue","newvalue");
}
}
}
// serialize the updated object back to the original file with the corrections/additions
System.IO.TextWriterwriter=newSystem.IO.StreamWriter(file);
serializer.Serialize(writer,mw);
writer.Close();
}
}
}

C# 3.0 version

[edit ]

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

usingSystem.IO;
usingSystem.Linq;
usingSystem.Xml;
usingSystem.Xml.Serialization;
namespaceWikiFileManipulation{
classProgram{
staticvoidMain(string[]args){
// name of the exported wiki file
varfile="ExportedWikiPages.xml";

// Open XML file containing exported wiki pages
varxml=newXmlDataDocument();
xml.Load(file);

// Deserialize the XML file into the MediaWikiType object
varserializer=newXmlSerializer(typeof(MediaWikiType));
varnodeReader=newXmlNodeReader(xml);
varmw=(MediaWikiType)serializer.Deserialize(nodeReader);

// Loop through all the RevisionType Items from each Page
foreach(varrinmw.page.SelectMany(p=>p.Items.OfType<RevisionType>())){
// increment the "timestamp" in order to re-import file
r.timestamp=r.timestamp.AddMinutes(1);

// Update each revision's text
r.text.Value=r.text.Value.Replace("oldvalue","newvalue");
}

// serialize the updates back to the same file
varwriter=newStreamWriter(file);
serializer.Serialize(writer,mw);
writer.Close();
}
}
}

AltStyle によって変換されたページ (->オリジナル) /