1

由于 MediaInfo v17 切换到 XSD 验证 XML,我正在尝试使用 xsd.exe 生成 C# 代码以反序列化 MediaInfo XML 文件。

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 工具:https ://docs.microsoft.com/en-us/dotnet/standard/serialization/xml-schema-definition-tool-xsd-exe

MediaInfo XSD 文件:https ://mediaarea.net/mediainfo/mediainfo_2_0.xsd

创建 C# 绑定(在 Visual Studio 2017 预览版中):

xsd.exe /c /namespace:MediaInfoXml /language:CS mediainfo_2_0.xsd

生成的 C# 文件包含轨道属性,片段:

/// <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))]

我从一个字符串中读取 XML,然后找到曲目,片段:

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; <--
    }
}

属性值显示在 Items[] 数组中,并且该属性的名称位于 ItemsElementName[] 数组中的相同数组索引处。

如何通过名称直接访问属性?

4

0 回答 0