XmlSerializer的。 跳过xml未知节点
我的xml文件反序列化有问题。 让我们假设我们有一个xml文件和一个我们用于反序列化的类。
例如:
xml –
newDataStore1 sdffasdfasdf Shapefile false newTestWorkspace false true ISO-8859-1 shapefile true file:data/shapefiles/states.shp https://www.opengeospatial.net/cite false
类
namespace GeoServerApiTester { /// [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlRootAttribute("dataStore", Namespace="", IsNullable=false)] public partial class DataStore { private string nameField; private string typeField; private bool enabledField; private WorkSpacePreview workspaceField; private ConnectionParametersStorageEntryCollection connectionParametersField; private string @__defaultField; private LinkCollection featureTypesField; /// [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0, ElementName="name")] public string Name { get { return this.nameField; } set { this.nameField = value; } } /// [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=1, ElementName="type")] public string Type { get { return this.typeField; } set { this.typeField = value; } } /// [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=2, ElementName="enabled")] public bool Enabled { get { return this.enabledField; } set { this.enabledField = value; } } /// [System.Xml.Serialization.XmlElementAttribute(Order=3, ElementName="workspace")] public WorkSpacePreview Workspace { get { return this.workspaceField; } set { this.workspaceField = value; } } /// [System.Xml.Serialization.XmlArrayAttribute(Order=4, ElementName="connectionParameters")] [System.Xml.Serialization.XmlArrayItemAttribute("entry", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] public ConnectionParametersStorageEntryCollection ConnectionParameters { get { return this.connectionParametersField; } set { this.connectionParametersField = value; } } /// [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=5)] public string @__default { get { return this.@__defaultField; } set { this.@__defaultField = value; } } /// [System.Xml.Serialization.XmlArrayAttribute(Order=6, ElementName="featureTypes")] [System.Xml.Serialization.XmlArrayItemAttribute("link", Namespace="https://www.w3.org/2005/Atom", IsNullable=false)] public LinkCollection FeatureTypes { get { return this.featureTypesField; } set { this.featureTypesField = value; } } public virtual bool ShouldSerializeConnectionParameters() { return ((this.ConnectionParameters != null) && (this.ConnectionParameters.Count > 0)); } public virtual bool ShouldSerializeFeatureTypes() { return ((this.FeatureTypes != null) && (this.FeatureTypes.Count > 0)); } } }
您可以看到该类不包含description字段。
newDataStore1 false
您可以看到描述后的所有元素都没有反序列化。
当程序获取xml内容并且此xml包含一个不在该类中的元素时,将不会预期此元素之后的所有元素。
如何在反序列化期间跳过未知元素并获得如下内容:
newDataStore1 Shapefile false newTestWorkspace false true ISO-8859-1 shapefile true file:data/shapefiles/states.shp https://www.opengeospatial.net/cite false
仅删除元素
默认情况下,XmlSerializer会忽略未知节点(因此也会忽略这些元素)。 现在,当您使用Order属性时,您明确告知要序列化/反序列化的顺序。
因此,当XmlSerializer来到您的description
元素时,这将成为一个未知元素,因为它需要type
元素。 其余的也将作为未知元素进行威胁,因为它们不再映射到您指定的顺序。 例如,当涉及到你的XML中具有索引2的type
元素时,它期望它是enabled
元素,因此该元素也变得未知。
您可以通过处理XmlSerializer类的UnknownNode事件来检查此行为。 将针对遇到的每个未知节点触发此事件。
如何进行? 如果排序没有意义,请不要使用它。 在某些情况下,使用排序确实有意义。 我多次见过的一个经典例子是(遗留)应用程序,它将XML文档视为字符串并从上到下读取所有元素。
另一种选择是实现IXmlSerializer接口,它可以更好地控制对象的序列化和反序列化方式。
我知道这不能回答你的问题,但我认为如果你改变方向,它将解决你的问题……
您是否创建了XSD来定义XML Schema? 如果没有,我建议从那里开始,然后使用xsd2code创建序列化类。
添加到Martijn的答案:
您还可以收集数组中的未知项目,以便稍后访问。
https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlanyelementattribute.aspx
上述就是C#学习教程:XmlSerializer的。 跳过xml未知节点分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
Public Class XClass ' Apply the XmlAnyElementAttribute to a field returning an array ' of XmlElement objects. Public AllElements() As XmlElement End Class 'XClass
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1012514.html