Framework is c# .net 4.6.2
I am generating automatic XML classes from XML codes
When I auto generate, it automatically converts as Array[][]
But i want to use it as List<List<>>
And i am sure that my conversation from Array to List causes some serialization error. I think it is about get and set functions. So i need your help to fix this issue
Here the auto generated code piece when i edit > paste special > paste XML as classes
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OxFordDefinition_perGroup
{
private string _GroupDescField;
private string _GroupSenseField;
private string _GroupGrammerField;
private OxFordDefinition_perGroup_perMainExample_perSubExample[][] _perMainExampleField;
/// <remarks/>
public string _GroupDesc
{
get
{
return this._GroupDescField;
}
set
{
this._GroupDescField = value;
}
}
/// <remarks/>
public string _GroupSense
{
get
{
return this._GroupSenseField;
}
set
{
this._GroupSenseField = value;
}
}
/// <remarks/>
public string _GroupGrammer
{
get
{
return this._GroupGrammerField;
}
set
{
this._GroupGrammerField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("_perSubExample", typeof(OxFordDefinition_perGroup_perMainExample_perSubExample), IsNullable = false)]
public OxFordDefinition_perGroup_perMainExample_perSubExample[][] _perMainExample
{
get
{
return this._perMainExampleField;
}
set
{
this._perMainExampleField = value;
}
}
}
But instead of arrays, i want to use List<List<>>
So i make it like below
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OxFordDefinition_perGroup
{
private string _GroupDescField;
private string _GroupSenseField;
private string _GroupGrammerField;
private List<List<OxFordDefinition_perGroup_perMainExample_perSubExample>> _perMainExampleField;
/// <remarks/>
public string _GroupDesc
{
get
{
return this._GroupDescField;
}
set
{
this._GroupDescField = value;
}
}
/// <remarks/>
public string _GroupSense
{
get
{
return this._GroupSenseField;
}
set
{
this._GroupSenseField = value;
}
}
/// <remarks/>
public string _GroupGrammer
{
get
{
return this._GroupGrammerField;
}
set
{
this._GroupGrammerField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("_perSubExample", typeof(OxFordDefinition_perGroup_perMainExample_perSubExample), IsNullable = false)]
public List<List<OxFordDefinition_perGroup_perMainExample_perSubExample>> _perMainExample
{
get
{
return this._perMainExampleField;
}
set
{
this._perMainExampleField = value;
}
}
}
But this time it gives serialization error when i try to serialize like below
public static string SerializeXML<T>(this T toSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringWriter textWriter = new StringWriter();
xmlSerializer.Serialize(textWriter, toSerialize);
return textWriter.ToString();
}
Here the full code of the XML class
Here the error it gives
Actually neither your original nor your modified
OxFordDefinition_perGroup
can be serialized successfully. The problem is your value forXmlArrayItem.Type
, which is the second argument to the constructor:According to the docs
The
typeof(OxFordDefinition_perGroup_perMainExample_perSubExample)
indicates that items in the outermost collection will be of typetypeof(OxFordDefinition_perGroup_perMainExample_perSubExample)
. However, in fact the items in the array or list are of typeOxFordDefinition_perGroup_perMainExample_perSubExample[]
orList<OxFordDefinition_perGroup_perMainExample_perSubExample>
respectively, which cannot, of course, be assigned to this type. ThisXmlSerializer
code generation fails.If you remove the type setting entirely from your
[XmlArrayItem]
attribute then both versions of your type will be serializable to XML:Sample fiddle.
Update
You asked, it adds extra layer which should not exists. any idea?
This is because you are using nested lists or jagged arrays. Change it to be a simple list or 1-d array:
Sample fiddle #2.
I then downloaded the entire XML from http://pastebin.com/raw/BJhRfFNf and ran
xsd.exe
to generate a schema, and then classes, from the XML, and I was able to reproduce your problem - incorrect classes were generated. I then manually changed the jagged array to a flat array (aList<T>
would work fine also) and was able to serialize and deserialize the XML without an exception getting thrown:Sample fiddle #3.
Unfortunately, it appears that only the first
</_perMainExample>
node is successfully deserialized using these tweaked classes, so at this point this auto-generated code just doesn't seem viable.I'm not sure why
xsd.exe
generated bad code here, you might want to ask another question or open an issue with Microsoft.Final Update
It looks as though
xsd.exe
(and thus Paste XML as Classes) is having trouble with a repeating element that contains nested repeating elements:I'm not sure what the problem is, but at this point I recommend switching to a different code-generation tool such as https://xmltocsharp.azurewebsites.net/, which generates the following classes:
Notice that:
The code generated is much, much cleaner.
An extra level of class
_perMainExample
is added to encapsulate the inner_perSubExample
list.Sample fiddle #4 which shows that the original and re-serialized XML are identical by calling
XNode.DeepEquals()
.