1

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?

PieterV
  • 555
  • 1
  • 4
  • 18
  • Does this answer your question? [xsd.exe generated c# with multiple elements in an array](https://stackoverflow.com/q/28836977). – dbc Nov 05 '17 at 22:15
  • That does look like the same problem, but my code is generated, and the template solution is cool, but it would be painful to go and add accessors for all the hundreds of items. For now I iterated through the arrays and created a dictionary of strings to objects. – PieterV Nov 06 '17 at 00:31
  • It would be simpler to create you dictionary directly from xml using xml linq then to get it from the generated classes. Would need sample of xml file (not xml schema) to write the code. When using xml serialization the attributes are simple properties of the class and can be either public or private. In you case it looks like they are private so you need a method in the class to get the private properties (or make the properties public). – jdweng Nov 06 '17 at 04:55
  • My current version is using LINQ, when MEdiaInfo changed the XML schema they added a namespace, and for the life of me I could not get my queries to work with a namespace. Tried to remove the namespace, fail, I then tried XSD to C#, seemed so easy, not really. I can go back to XML as MediaInfo does support an OLDXML switch, but then I miss out on some new attributes. Example: https://1drv.ms/u/s!AqeqhT0NczrNh9BzFQ4K0BJEjhPr4A – PieterV Nov 06 '17 at 15:19
  • I found that http://xmltocsharp.azurewebsites.net/ created code that I could understand and easily edit to my needs. I did use a sample XML file, but I just need a couple feelds, so no need to support the entire XSD. – PieterV Nov 06 '17 at 22:57

0 Answers0