Question on deserialization

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi All,
I have following situation:<br/>
Given xml:
<?xml version="1.0" encoding="utf-8"?><br/>
<RESULT><br/>
<RESULT_ITEM1><br/>
<ZAHL format="format1" color="color1 <br/>
<VALUE>1</VALUE><br/>
</ZAHL><br/>
<BETRAG format="format1" color="color1 <br/>
<VALUE>1</VALUE><br/>
</BETRAG><br/>
<ZAHL format="format2" color="color2 <br/>
<VALUE>-3.40282347E+38</VALUE><br/>
</ZAHL><br/>
<DATUM format="format1" color="color1 <br/>
<VALUE>VALUE1</VALUE><br/>
</DATUM><br/>
<DATUM format="format2" color="color2 <br/>
<VALUE>VALUE2</VALUE><br/>
</DATUM><br/>
</RESULT_ITEM1><br/>
<RESULT_ITEM1><br/>
<BETRAG format="format1" color="color1 <br/>
<VALUE>1</VALUE><br/>
</BETRAG><br/>
<BETRAG format="format1" color="color1 <br/>
<VALUE>1</VALUE><br/>
</BETRAG><br/>
</RESULT_ITEM1><br/>
<RESULT_ITEM1><br/>
<DATUM format="format1" color="color1 <br/>
<VALUE>1</VALUE><br/>
</DATUM><br/>
</RESULT_ITEM1><br/>
<RESULT_ITEM1><br/>
<ZAHL format="format1" color="color1 <br/>
<VALUE>1</VALUE><br/>
</ZAHL><br/>
</RESULT_ITEM1><br/>
</RESULT>.
The field in the third level of xml-tree (in my example ZAHL, DATUM, BETRAG) are of same structure (2 Attributes and 1 Element "VALUE").<br/>
All the names of element (RESULT, RESULT_ITEM1, ZAHL, DATUM, BETRAG) are dynamic values. I need to deserialize this xml.
So far my code:
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();<br/>
XmlAttributes attrs = new XmlAttributes();<br/>
XmlElementAttribute attr = new XmlElementAttribute();<br/>
attr.ElementName = "RESULT_ITEM1";<br/>
attr.Type = typeof(RESULTRESULT_ITEM);<br/>
attrOverrides.Add(typeof(RESULT), "RESULT_ITEM", attrs);<br/>
// Create the XmlSerializer using the XmlAttributeOverrides.<br/>
XmlSerializer s = new XmlSerializer(typeof(RESULT), attrOverrides,<br/>
new Type[] { typeof(int) }, new XmlRootAttribute("RESULT"),
null);
FileStream fs = new FileStream(<my filename>, FileMode.Open);<br/>
RESULT res = (RESULT)s.Deserialize(fs);
The type RESULT was generated with xsd utility (skiped for brevity).<br/>
I need somehow to substitute in the generated code
<div style="color:Black;background-color:White; <pre>
[System.CodeDom.Compiler.GeneratedCodeAttribute(<span style="color:#A31515; "xsd", <span style="color:#A31515; "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(<span style="color:#A31515; "code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=<span style="color:Blue; true)]
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class RESULTRESULT_ITEM {

<span style="color:Blue; private <span style="color:Blue; object[] itemsField;

<span style="color:Gray; /// <span style="color:Gray; <remarks/>
[System.Xml.Serialization.XmlElementAttribute(<span style="color:#A31515; "BETRAG", <span style="color:Blue; typeof(RESULTRESULT_ITEMBETRAG))]
[System.Xml.Serialization.XmlElementAttribute(<span style="color:#A31515; "DATUM", <span style="color:Blue; typeof(RESULTRESULT_ITEMDATUM))]
[System.Xml.Serialization.XmlElementAttribute(<span style="color:#A31515; "ZAHL", <span style="color:Blue; typeof(RESULTRESULT_ITEMZAHL))]
<span style="color:Blue; public <span style="color:Blue; object[] Items {
<span style="color:Blue; get {
<span style="color:Blue; return <span style="color:Blue; this.itemsField;
}
<span style="color:Blue; set {
<span style="color:Blue; this.itemsField = value;
}
}
}
[/code]
the [XmlElementAttribute] through corresponded C# code.
Thanks a lot for help,
Alex

View the full article
 
Back
Top