C# Deserializing an XML with repeating tags -
i have input xml string repeating <row>
tags i'm trying deserialize object, rows except last 1 ignored. appreciated.
as example, after deserialization, object is:
object.command[0].usertable = {oci.ocitable} colheading: {string[3]} colheadingfield: {string[3]} row: {string[3]} rowfield: {string[3]}
this wrong, because there 1 row in object, there should 4 <row>
in input xml string this:
<?xml version="1.0" encoding="iso-8859-1" ?> <broadsoftdocument protocol="oci" xmlns="c" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <sessionid xmlns="">feajiofefeaij</sessionid> <command echo="" xsi:type="usergetlistinserviceproviderresponse" xmlns=""> <usertable> <colheading>user id</colheading> <colheading>group id</colheading> <colheading>name</colheading> <row> <col>1</col> <col>a</col> <col>smith</col> </row> <row> <col>2</col> <col>a</col> <col>john</col> </row> <row> <col>3</col> <col>b</col> <col>james</col> </row> <row> <col>4</col> <col>b</col> <col>lisa</col> </row> </usertable> </command> </broadsoftdocument>
the way i'm doing deserialization is:
memorystream memstream = new memorystream(encoding.utf8.getbytes(responsestring)); xmlserializer ser = new xmlserializer(typeof(ocimessage)); ocimessage response = (ocimessage)ser.deserialize(memstream);
and c# class automatically generated xsd.exe
ocitable
class this:
[system.codedom.compiler.generatedcodeattribute("xsd", "4.0.30319.1")] [system.serializableattribute()] [system.diagnostics.debuggerstepthroughattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(namespace = "c")] public partial class ocitable { private string[] colheadingfield; private string[] rowfield; [system.xml.serialization.xmlelementattribute("colheading", form = system.xml.schema.xmlschemaform.unqualified)] public string[] colheading { { return this.colheadingfield; } set { this.colheadingfield = value; } } [system.xml.serialization.xmlarrayattribute(form = system.xml.schema.xmlschemaform.unqualified)] [system.xml.serialization.xmlarrayitemattribute("col", typeof(string), form = system.xml.schema.xmlschemaform.unqualified, isnullable = false)] public string[] row { { return this.rowfield; } set { this.rowfield = value; } } }
the main problem that, in xml, <row>
, <col>
elements 2d jagged array: there outer, repeating set of <row>
elements containing inner, repeating set of <col>
elements, each of has string value. in ocitable
class have modeled 1d array of strings:
public string[] row { get; set; }
this allows one outer <row>
element. if multiple <row>
elements encountered while parsing, xmlserializer
overwrite earlier values later - seeing.
in order capture nested hierarchy of row , column elements, instead need like:
public string[][] row { get; set; }
however, if do, encounter problem described in c# xml serialization remove jagged array element name, namely xmlserializer
serialize 2d jagged array outer container element, so:
<row> <col> <string>a</string> </col> <col> <string>b</string> </col> </row>
thus not work.
instead, need make row
member simple array of intermediate classes contain 1d array of strings, so:
[system.codedom.compiler.generatedcodeattribute("xsd", "4.0.30319.1")] [system.serializableattribute()] [system.diagnostics.debuggerstepthroughattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(namespace = "c")] public partial class ocitable { private string[] colheadingfield; [system.xml.serialization.xmlelementattribute("colheading", form = system.xml.schema.xmlschemaform.unqualified)] public string[] colheading { { return this.colheadingfield; } set { this.colheadingfield = value; } } [system.xml.serialization.xmlelement(form = system.xml.schema.xmlschemaform.unqualified)] public ocitablerow [] row { get; set; } } public class ocitablerow { [system.xml.serialization.xmlelementattribute("col", form = system.xml.schema.xmlschemaform.unqualified, isnullable = false)] public string[] col { get; set; } }
both row
, col
arrays marked [xmlelement]
indicate should serialized repeating sequence of elements without outer container element.
with classes defined above able deserialize <usertable>
element. sample fiddle.
Comments
Post a Comment