在反序列化之前如何区分XML文件的类型

本文关键字:XML 文件 类型 何区 反序列化 | 更新日期: 2023-09-27 18:03:18

我正在将musicxml文件加载到程序中。问题:有两种"方言",时间方向部分方向,它们有不同的根节点(和不同的结构):

<?xml version="1.0" encoding='UTF-8' standalone='no' ?>
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 2.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise version="2.0">
    <work>...</work>
    ...
</score-partwise>

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE score-timewise PUBLIC "-//Recordare//DTD MusicXML 2.0 Timewise//EN" "http://www.musicxml.org/dtds/timewise.dtd">
<score-timewise version="2.0">
   <work>...</work>
   ...
</score-timewise>
到目前为止,我反序列化部分分数的代码是:
using (var fileStream = new FileStream(openFileDialog.FileName, FileMode.Open))
{
    var xmlSerializer = new XmlSerializer(typeof(ScorePartwise));
    var result = (ScorePartwise)xmlSerializer.Deserialize(fileStream);
}

区分这两种方言的最好方法是什么?

在反序列化之前如何区分XML文件的类型

方法如下:使用XDocument解析文件,读取根元素以确定类型,并将其读入序列化器。

var xdoc = XDocument.Load(filePath);
Type type;
if (xdoc.Root.Name.LocalName == "score-partwise")
    type = typeof(ScorePartwise);
else if (xdoc.Root.Name.LocalName == "score-timewise")
    type = typeof(ScoreTimewise);
else
    throw new Exception();
var xmlSerializer = new XmlSerializer(type);
var result = xmlSerializer.Deserialize(xdoc.CreateReader());

我将创建两个序列化器

var partwiseSerializer = new XmlSerializer(typeof(ScorePartwise));
var timewiseSerializer = new XmlSerializer(typeof(ScoreTimewise));

假设只有这两个,我将在一个

上调用 candeseralize 方法
using (var fileStream = new FileStream(openFileDialog.FileName, FileMode.Open))
{
  using (var xmlReader = XmlReader.Create(filStream))
  {
    if (partwiseSerializer.CanDeserialize(xmlReader))
    {
       var result = partwiseSerializer.Deserialize(xmlReader);
    }
    else
    {
       var result = timewiseSerializer.Deserialize(xmlReader);
    }
  }
}

显然这只是一个如何做的想法。如果有更多的选择,或者根据您的应用程序设计,我会使用一种更复杂的方法来调用 candeseralize ,但在我看来,这种方法是关键:

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.candeserialize.aspx

XmlReader类可以在这里找到:

http://msdn.microsoft.com/en-us/library/System.Xml.XmlReader (v = vs.110) . aspx

如果您关心资源使用:

    internal const string NodeStart = "<Error ";
    public static bool IsErrorDocument(string xml)
    {
        int headerLen = 1;
        if (xml.StartsWith(Constants.XMLHEADER_UTF8))
        {
            headerLen += Constants.XMLHEADER_UTF8.Length;
        }
        else if (xml.StartsWith(Constants.XMLHEADER_UTF16))
        {
            headerLen += Constants.XMLHEADER_UTF16.Length;
        }
        else
        {
            return false;
        }
        if (xml.Length < headerLen + NodeStart.Length)
        {
            return false;
        }
        return xml.Substring(headerLen, NodeStart.Length) == NodeStart;
    }
internal class Constants
{
    public const string XMLHEADER_UTF16 = "<?xml version='"1.0'" encoding='"utf-16'"?>";
    public const string XMLHEADER_UTF8 = "<?xml version='"1.0'" encoding='"utf-8'"?>";
}