I have a problem with deserializing my xml files. Let them pretend that we have an xml file and a class that we use for deserialization.
For instance:
xml -
<dataStore>
<name>newDataStore1</name>
<description>sdffasdfasdf</description>
<type>Shapefile</type>
<enabled>false</enabled>
<workspace>
<name>newTestWorkspace</name>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="ht
tp://192.168.6.71:8080/geoserver/rest/workspaces/newTestWorkspace.xml" type="app
lication/xml"/>
</workspace>
<connectionParameters>
<entry key="memory mapped buffer">false</entry>
<entry key="create spatial index">true</entry>
<entry key="charset">ISO-8859-1</entry>
<entry key="filetype">shapefile</entry>
<entry key="cache and reuse memory maps">true</entry>
<entry key="url">file:data/shapefiles/states.shp</entry>
<entry key="namespace">http://www.opengeospatial.net/cite</entry>
</connectionParameters>
<__default>false</__default>
<featureTypes>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="ht
tp://192.168.6.71:8080/geoserver/rest/workspaces/newTestWorkspace/datastores/new
DataStore1/featuretypes.xml" type="application/xml"/>
</featureTypes>
</dataStore>
Class
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="http://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));
}
}
}
You can see that the class does not contain a description field.
<dataStore>
<name>newDataStore1</name>
<enabled>false</enabled>
</dataStore>
You can see that all the elements after the description were not deserialized.
When the program receives the contents of xml, and this xml contains an element that is not part of the class, all elements after this element is not deserialized.
How to skip an unknown element during deserialization and get something like this:
<dataStore>
<name>newDataStore1</name>
<type>Shapefile</type>
<enabled>false</enabled>
<workspace>
<name>newTestWorkspace</name>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="ht
tp://192.168.6.71:8080/geoserver/rest/workspaces/newTestWorkspace.xml" type="app
lication/xml"/>
</workspace>
<connectionParameters>
<entry key="memory mapped buffer">false</entry>
<entry key="create spatial index">true</entry>
<entry key="charset">ISO-8859-1</entry>
<entry key="filetype">shapefile</entry>
<entry key="cache and reuse memory maps">true</entry>
<entry key="url">file:data/shapefiles/states.shp</entry>
<entry key="namespace">http://www.opengeospatial.net/cite</entry>
</connectionParameters>
<__default>false</__default>
<featureTypes>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="ht
tp://192.168.6.71:8080/geoserver/rest/workspaces/newTestWorkspace/datastores/new
DataStore1/featuretypes.xml" type="application/xml"/>
</featureTypes>
</dataStore>
delete only item