使用或不使用命名空间反序列化 XML
Deserialize XML either with or without Namespace
我收到了几个 xml
文件。有些有命名空间,有些没有。如何在不出现异常的情况下准确地转换这些。
这是我的代码:
SASBM XML:
<ExportData xmlns="http://www.w3.org/2001/XMLSchema-instance">
<SASBM>...</SASBM>
</ExportData>
SASCS XML:
<ExportData>
<SASCS>...</SASCS>
</ExportData>
我的XML解串器:
_xmlSerializer = new XmlSerializer(typeof(ExportData));
_xmlSerializer.Deserialize(sr);
var tst = _xmlSerializer.Deserialize(sr);
还有我的 class:
/// <remarks/>
[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
[System.Xml.Serialization.XmlRoot(Namespace = "http://www.w3.org/2001/XMLSchema-instance", IsNullable = false)]
public partial class ExportData
{
/// <remarks/>
public byte PlantCode { get; set; }
/// <remarks/>
public byte ServerID { get; set; }
/// <remarks/>
public uint MessageNumber { get; set; }
/// <remarks/>
public ExportDataSASAH SASAH { get; set; }
/// <remarks/>
public ExportDataSASCS SASCS { get; set; }
/// <remarks/>
public ExportDataSASBM SASBM { get; set; }
}
将 namespace
设置为 namespace=""
修复了一种文件类型,但我在第二种文件类型上遇到异常。而且我不知道添加多个命名空间选项的方法。
解决方案是覆盖 XMLSerializer 命名空间函数
// helper class to ignore namespaces when de-serializing
public class NamespaceIgnorantXmlTextReader : XmlTextReader
{
public NamespaceIgnorantXmlTextReader(TextReader reader) : base(reader)
{
}
public override string NamespaceURI
{
get { return ""; }
}
}
然后删除命名空间属性:
[System.Xml.Serialization.XmlRoot(Namespace = "http://www.w3.org/2001/XMLSchema-instance", IsNullable = false)]
最后解析如下:
_xmlSerializer.Deserialize(new NamespaceIgnorantXmlTextReader(sr));
我收到了几个 xml
文件。有些有命名空间,有些没有。如何在不出现异常的情况下准确地转换这些。
这是我的代码:
SASBM XML:
<ExportData xmlns="http://www.w3.org/2001/XMLSchema-instance">
<SASBM>...</SASBM>
</ExportData>
SASCS XML:
<ExportData>
<SASCS>...</SASCS>
</ExportData>
我的XML解串器:
_xmlSerializer = new XmlSerializer(typeof(ExportData));
_xmlSerializer.Deserialize(sr);
var tst = _xmlSerializer.Deserialize(sr);
还有我的 class:
/// <remarks/>
[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
[System.Xml.Serialization.XmlRoot(Namespace = "http://www.w3.org/2001/XMLSchema-instance", IsNullable = false)]
public partial class ExportData
{
/// <remarks/>
public byte PlantCode { get; set; }
/// <remarks/>
public byte ServerID { get; set; }
/// <remarks/>
public uint MessageNumber { get; set; }
/// <remarks/>
public ExportDataSASAH SASAH { get; set; }
/// <remarks/>
public ExportDataSASCS SASCS { get; set; }
/// <remarks/>
public ExportDataSASBM SASBM { get; set; }
}
将 namespace
设置为 namespace=""
修复了一种文件类型,但我在第二种文件类型上遇到异常。而且我不知道添加多个命名空间选项的方法。
解决方案是覆盖 XMLSerializer 命名空间函数
// helper class to ignore namespaces when de-serializing
public class NamespaceIgnorantXmlTextReader : XmlTextReader
{
public NamespaceIgnorantXmlTextReader(TextReader reader) : base(reader)
{
}
public override string NamespaceURI
{
get { return ""; }
}
}
然后删除命名空间属性:
[System.Xml.Serialization.XmlRoot(Namespace = "http://www.w3.org/2001/XMLSchema-instance", IsNullable = false)]
最后解析如下:
_xmlSerializer.Deserialize(new NamespaceIgnorantXmlTextReader(sr));