Since MediaInfo v17 switched to XSD validated XML, I am trying to use xsd.exe to generate C# code to deserialize the MediaInfo XML file.
https://mediaarea.net/MediaInfo/ChangeLog
Version 17.10, 2017-11-02
+ New MediaInfo XML output, with XSD, more suitable for automatic parsing. Use Option("Inform", "OLDXML") for keeping previous behavior
XSD Tool: https://learn.microsoft.com/en-us/dotnet/standard/serialization/xml-schema-definition-tool-xsd-exe
MediaInfo XSD file: https://mediaarea.net/mediainfo/mediainfo_2_0.xsd
Create C# bindings (in Visual Studio 2017 Preview):
xsd.exe /c /namespace:MediaInfoXml /language:CS mediainfo_2_0.xsd
The resulting C# file contains the track attributes, snippet:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="https://mediaarea.net/mediainfo")]
public partial class trackType {
private object[] itemsField;
private ItemsChoiceType[] itemsElementNameField;
private extraType extraField;
private string typeField;
private string typeorderField;
public trackType() {
this.typeorderField = "1";
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Accompaniment", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("ActiveFormatDescription", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Actor", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Actor_Character", typeof(string))]
I read the XML from a string, then find the tracks, snippet:
static public MediaInfoXml.mediainfoType FromXml(string xml)
{
XmlSerializer xmlserializer = new XmlSerializer(typeof(MediaInfoXml.mediainfoType));
TextReader textreader = new StringReader(xml);
return xmlserializer.Deserialize(textreader) as MediaInfoXml.mediainfoType;
}
...
MediaInfoXml.mediainfoType xmlinfo = MediaInfoTool.FromXml(xml);
MediaInfoXml.mediaType xmlmedia = xmlinfo.Items.First() as MediaInfoXml.mediaType;
foreach (MediaInfoXml.trackType track in xmlmedia.track)
{
if (track.type.Equals("Video", StringComparison.OrdinalIgnoreCase))
{
var attrib = track.Items[0];
var attrname = track.ItemsElementName[0];
// [System.Xml.Serialization.XmlElementAttribute("Language", typeof(string))]
// --> string language = track.Language; <--
}
}
The attribute value shows up in the Items[] array, and that name of the attribute at the same array index in the ItemsElementName[] array.
How do I access the attributes directly by name?