c# - xmlSerializer.Deserialize poorly formed XML -
i'm attempting deserialize xml (below)
i'm stuck due "non-standard" structure of xml. instead of normal way serialize products collection, ie: parent many child elements. have many parent elements each single child element.
my issue due unusual array structure (i suspect due bug), can't figure out how set attributes ie:[xmlarrayitem] can extract meaningful data.
note: xml httpresponse public 3rd party. (so can't them change it.)
specifically: instead of < products> parent many < product> elements.
the < bettype> node has multiple < products> parent elements each single < product> child element.
you have total freedom create whatever classes whatever properties needed. solution needs bettype & product classes. i've tried both & without products class.
<rootnode> : <bet_types> <bet_type id="105"> <name>exacta</name> <products> <product id="17"> <name>stab</name> <max_stake>10000</max_stake> <allow_multiple>0</allow_multiple> <allow_flexi>1</allow_flexi> <product_default>1</product_default> </product> </products> <products> <product id="25"> <name>nsw</name> <max_stake>10000</max_stake> <allow_multiple>0</allow_multiple> <allow_flexi>1</allow_flexi> </product> </products> </bet_type> <bet_type id="107"> <name>quinella</name> <products> <product id="18"> <name>stab</name> <max_stake>10000</max_stake> <allow_multiple>0</allow_multiple> <allow_flexi>1</allow_flexi> <product_default>1</product_default> </product> </products> <products> <product id="26"> <name>nsw</name> <max_stake>10000</max_stake> <allow_multiple>0</allow_multiple> <allow_flexi>1</allow_flexi> </product> </products> </bet_type> : </rootnode>
ideally use .net c# xmlserializer i'm using other calls. , sample small fragment nested deep in httpresponse.
one possible alternative use xslt reformat it, hoping there way attributes. think cleaner, & i'm unsure how i'd write xslt it.
note: alternatively if can suggest way "not produce" node parent array & create node each array item, help. 1 of approaches i've tried got close. still had "products" node parent multiple products nodes each contained single product node.
thanks help.
when in doubt, create xml schema xml document , run xsd.exe
on it. can @ (or use) generated code.
to started, here's xml schema matches xml posted above. run xsd.exe /c /f /n:your.namespace.here filename.xsd
generate code.
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/xmlschema" elementformdefault="qualified" > <xs:element name="rootnode"> <xs:complextype> <xs:sequence> <xs:element name="bet_types"> <xs:complextype> <xs:sequence> <xs:element name="bet_type" minoccurs="0" maxoccurs="unbounded"> <xs:complextype> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="products" minoccurs="0" maxoccurs="unbounded"> <xs:complextype> <xs:sequence> <xs:element name="product"> <xs:complextype> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="max_stake" type="xs:int" /> <xs:element name="allow_multiple" type="xs:int" /> <xs:element name="allow_flexi" type="xs:int" /> <xs:element name="product_default" type="xs:int" minoccurs="0" /> </xs:sequence> <xs:attribute name="id" type="xs:int" use="required" /> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> <xs:attribute name="id" type="xs:int" use="required" /> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:schema>
Comments
Post a Comment