If you are writing a C# program that consumes MusicXML files (a digital format for sheet music), it may be helpful to deserialize MusicXML files to a strongly typed class instead of manually parsing the XML document.
These C# classes help you do that. They are auto-generated from the official MusicXML schema definition files using the XmlSchemaClassGenerator project.
...
...
...
/// <summary>
/// <para>The score-partwise element is the root element for a partwise MusicXML score. It includes a score-header group followed by a series of parts with measures inside. The document-attributes attribute group includes the version attribute.</para>
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "2.0.0.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute("score-partwise", Namespace="", AnonymousType=true)]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("score-partwise", Namespace="")]
public partial class ScorePartwise
{
[System.Xml.Serialization.XmlElementAttribute("work", Namespace="")]
public Work Work { get; set; }
/// <summary>
/// <para>The movement-number element specifies the number of a movement.</para>
/// </summary>
[System.Xml.Serialization.XmlElementAttribute("movement-number", Namespace="")]
public string MovementNumber { get; set; }
/// <summary>
/// <para>The movement-title element specifies the title of a movement, not including its number.</para>
/// </summary>
[System.Xml.Serialization.XmlElementAttribute("movement-title", Namespace="")]
public string MovementTitle { get; set; }
...
...
...
28,500 more lines...
I briefly tested each C# class against one MusicXML file sourced from MuseScore.org (ClassicMan's transcription of Chopin's Scherzo No. 2 Op. 31):
using System;
using System.Xml.Serialization;
using System.IO;
namespace ExampleProgram
{
static class Program
{
static void Main()
{
XmlSerializer serializer = new XmlSerializer(typeof(MusicXmlSchema.ScorePartwise));
// Uncompress the MusicXML file if it ends with a .mxl before passing it in
string filename = "scherzo-no-2-opus-31.xml";
// Your fully typed score
var score = serializer.Deserialize(new FileStream(filename, FileMode.Open)) as MusicXmlSchema.ScorePartwise;
Console.WriteLine(score.Credit[0].CreditWords.Value);
// Output: Scherzo No. 2 in B♭ Minor
Console.WriteLine(score.Part[0].Measure[0].Note[0].Pitch.Step);
// Output: B
Console.ReadKey();
}
}
}
Each folder contains:
- The original MusicXML schema definition files as downloaded from musicxml.com for that version
- The converted XSD -> C# class for XML deserialization
- Run XmlSchemaClassGenerator on each MusicXML version of the
musicxml.xsd
files. - Manually remove some extra generated classes
X_1
,X_2
(there were 19 instances of these extra classes).
Thanks to XmlSchemaClassGenerator for a great conversion library.
I tried other tools like .NET's built-in xsd.exe
and the commercial xsd2code
, but both xsd.exe
and xsd2code
do not pascal case names (they capitalizes the first letter but doesn't pascal case according to the dashes in element names).
The original XmlSchemaClassGenerator
uses _
underscores between words instead of plain pascal casing so this fork has a quick patch to remove the _
to allow plain pascal casing.
The generated C# classes also contained some repeat _1 _2 _3 suffix classes that caused errors when I tried the usage example so I manually removed those.