Groups | Blog | Home
all groups > dotnet xml > december 2004 >

dotnet xml : Deserialization issue


Mike Sarbu
12/21/2004 10:58:35 PM
Hello all,

I have an XML file like this:

<?xml version="1.0" encoding="utf-8"?>
<SomeObject xmlns="http://www.abcinc.com/objectdefinition"
xmlns:someobj=http://www.abcinc.com/objectdefinition>
......
</SomeObject>

And I want to deserialize it, so I created a class:

[XmlRoot(Namespace="someobj", ElementName="SomeObject")]
class SomeObject
{
....
}

When calling the 'Deserialize' method of a serializer I get an error
"There's an error in the XML document (2,2)".

If I don't use namespaces (which I cannot aford, since I get the file from a
vendor), everything works fine:

<?xml version="1.0" encoding="utf-8"?>
<SomeObject>
......
</SomeObject>

Deserialize without issues into:

[XmlRoot]
class SomeObject
{
....
}

Could somebody let me know what am I doing wrong? I even tried to add the
namespace in the class' constructor, with the same result, like this:

[XmlRoot(Namespace="someobj", ElementName="SomeObject")]
class SomeObject
{
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
public SomeObject()
{
xmlns.Add( "", @"http://www.abcinc.com/objectdefinition" );
xmlns.Add( "someobj", @http://www.abcinc.com/objectdefinition );
}
}

Thanks a lot!

Mike

Dino Chiesa [Microsoft]
12/23/2004 11:51:58 AM
Mike,
to solve these sorts of problems, you can try working in the opposite
direction. When I want to de-serialize a document of a particular schema,
here's what I do:

1. infer the schema from the document using xsd.exe (only necessary if no
schema is available. Really, the doc publisher should give you a schema,
you shouldn't have to infer it)

2. generate classes from that schema, using xsd.exe /c

3. build a test driver (20 lines of code) that instantiates the class and
then serializes it. Compare to the xml document you are trying to match.
You can then iterate on your schema or the generated class, tweaking it to
match what you want.


-----

In your case,

A. the
xmlns:someobj=http://www.abcinc.com/objectdefinition
is not valid XML. Need quotes around this namespace value.

B. you did not specify the rest of the XML doc, but the assignment of the
someobj prefix corresponding to the http://www.abcinc.com/objectdefinition
namespace seems to be unnecessary. This will not break your
de-serialization, but it is superfluous.

C. the class I got by following the steps above was this:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.abcinc.com/objectdefinition")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.abcinc.com/objectdefinition",
IsNullable=false)]
public class SomeObject {
public string SomeField;
}


And it de-serialized your XML (with the necessary quotes added, and one
element added for illustration) just fine.

D. to produce a similar XML doc by serializing, use
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add( "", "http://www.abcinc.com/objectdefinition" ); // this will be
the default (no prefix) namespace in the XML doc, eg, xmlns="..."

before serializing, and be sure to specify this ns in the Serialize() step.

s1.Serialize(xmlWriter, so, ns);

-Dino




[quoted text, click to view]


begin 666 miketest.cs
M=7-I;F<@4WES=&5M+DE/.PT*=7-I;F<@4WES=&5M+EAM;"Y397)I86QI>F%T
M:6]N.PT*#0IN86UE<W!A8V4@26]N:6,@>PT*#0H@('!U8FQI8R!C;&%S<R!8
M;6Q497AT5W)I=&5R1F]R;6%T=&5D3F]$96-L87)A=&EO;B Z(%-Y<W1E;2Y8
M;6PN6&UL5&5X=%=R:71E<B![#0H@(" @<'5B;&EC(%AM;%1E>'17<FET97)&
M;W)M871T961.;T1E8VQA<F%T:6]N("A3>7-T96TN24\N5&5X=%=R:71E<B!W
M*2 Z(&)A<V4H=RD@>R!&;W)M871T:6YG/2!3>7-T96TN6&UL+D9O<FUA='1I
M;F<N26YD96YT960[?0T*(" @('!U8FQI8R!O=F5R<FED92!V;VED(%=R:71E
M4W1A<G1$;V-U;65N=" H*2![('T-"B @?0T*#0H-"@T*("!P=6)L:6,@8VQA
M<W,@5&5S=$1R:79E<B![#0H-"B @("!S=&%T:6,@=F]I9"!-86EN*'-T<FEN
M9UM=(&%R9W,I('L-"@T*(" @(" @=')Y('L-"@T*"5-Y<W1E;2Y)3RY3=')I
M;F=7<FET97(@<W1R:6YG5W)I=&5R.R -"@E3>7-T96TN6&UL+EAM;%=R:71E
M<B!X;6Q7<FET97([( T*#0H)+R\@<V5T=7 -"@E8;6Q397)I86QI>F5R(',Q
M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L-"GT-"@T*
`
end

begin 666 mike.xml
M/%-O;65/8FIE8W0@>&UL;G,](FAT=' Z+R]W=W<N86)C:6YC+F-O;2]O8FIE
M8W1D969I;FET:6]N(B!X;6QN<SIS;VUE;V)J/2)H='1P.B\O=W=W+F%B8VEN
M8RYC;VTO;V)J96-T9&5F:6YI=&EO;B(^#0H@(#Q3;VUE1FEE;&0^=F%L=64@
F9V]E<R!H97)E/"]3;VUE1FEE;&0^#0H\+U-O;65/8FIE8W0^#0H`
`
end
Christoph Schittko [MVP]
12/23/2004 1:23:30 PM
Mike and Dino,

Inferring schemas and classes is definitely a good way to go. Note that
there are newer tools on GDN for inferring schemas from classes [0] and
creating classes from schemas [1]. These tools should address some of
the shortcomings of xsd.exe.

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko


[0] http://apps.gotdotnet.com/xmltools/xsdinference/
[1] http://apps.gotdotnet.com/xmltools/xsdobjgen/

[quoted text, click to view]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.abcinc.
co
[quoted text, click to view]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.abcinc.
co
[quoted text, click to view]

Christoph Schittko [MVP]
12/27/2004 10:15:05 PM
Can you post what XML files your dealing with? You said that they are
generated by a Microsoft tool. Could you say which tool?

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko


[quoted text, click to view]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.abcinc.
co
[quoted text, click to view]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.abcinc.
co
[quoted text, click to view]

Mike Sarbu
12/27/2004 10:46:22 PM
Hi Dino,

Thanks a lot for the pointers you gave me, they were great. Unfortunately, I
still have problems.

Sorry for the missing quotes in the example that I posted, I typed it
instead of doing a copy-paste. In the real file the quotes were present.

Now about my issues:

1. The publisher (which is Microsoft) didn't publish the schema.
2. xsd.exe cannot infer the schema from the xml file because of an error in
the xml document: "The same table (somenamehere) cannot be the child table
in two nested relations". Now the xml file is generated by a Microsoft tool,
so I would've expected it to be correct ...
3. Even when I delete the offending elements, I can succesfully generate the
schema, I then generate the class, which looks identical to the one you've
generated, but I get the same error when deserializing it.

Thanks a lot for any other piece of advice that may help,

Mike

[quoted text, click to view]

Mike Sarbu
12/27/2004 11:40:21 PM
Well, I'm trying to deserialize an RDL file, generated by Visual Studio.
Please find the file attached.

Thanks,
Mike


[quoted text, click to view]


begin 666 test.xml
M/#]X;6P@=F5R<VEO;CTB,2XP(B!E;F-O9&EN9STB=71F+3@B/SX-"CQ297!O
M<G0@>&UL;G,](FAT=' Z+R]S8VAE;6%S+FUI8W)O<V]F="YC;VTO<W%L<V5R
M=F5R+W)E<&]R=&EN9R\R,# S+S$P+W)E<&]R=&1E9FEN:71I;VXB('AM;&YS
M.G)D/2)H='1P.B\O<V-H96UA<RYM:6-R;W-O9G0N8V]M+U-13%-E<G9E<B]R
M97!O<G1I;F<O<F5P;W)T9&5S:6=N97(B/@T*(" \4FEG:'1-87)G:6X^,6EN
M/"]2:6=H=$UA<F=I;CX-"B @/$)O9'D^#0H@(" @/%)E<&]R=$ET96US/@T*
M(" @(" @/%1A8FQE($YA;64](G1A8FQE0W5S=&]M(CX-"B @(" @(" @/$AE
M:6=H=#XP+C<U:6X\+TAE:6=H=#X-"B @(" @(" @/%-T>6QE("\^#0H@(" @
M(" @(#Q(96%D97(^#0H@(" @(" @(" @/%1A8FQE4F]W<SX-"B @(" @(" @
M(" @(#Q486)L95)O=SX-"B @(" @(" @(" @(" @/$AE:6=H=#XP+C(U:6X\
M+TAE:6=H=#X-"B @(" @(" @(" @(" @/%1A8FQE0V5L;',^#0H@(" @(" @
M(" @(" @(" @/%1A8FQE0V5L;#X-"B @(" @(" @(" @(" @(" @(#Q297!O
M<G1)=&5M<SX-"B @(" @(" @(" @(" @(" @(" @/%1E>'1B;W@@3F%M93TB
M86-C;W5N=%]I9"(^#0H@(" @(" @(" @(" @(" @(" @(" @/%-T>6QE/@T*
M(" @(" @(" @(" @(" @(" @(" @(" @/%!A9&1I;F=,969T/C)P=#PO4&%D
M9&EN9TQE9G0^#0H@(" @(" @(" @(" @(" @(" @(" @(" \5&5X=$%L:6=N
Christoph Schittko [MVP]
12/28/2004 8:21:49 PM
Mike,

The XML you posted deserializes fine with classes generated with the
XSDObjectGen tool [0] I mentioned in my first post. I added a
schemaLocation attribute to schema 0:

<xs:import
namespace="http://schemas.microsoft.com/SQLServer/reporting/reportdesign
er" schemaLocation="schema1.xml" />

for the tool to pick up the second schema when generating the classes.

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko






[0] http://apps.gotdotnet.com/xmltools/xsdobjgen/

[quoted text, click to view]
Mike Sarbu
12/29/2004 12:14:42 AM
Thank you very much, I can see that works fine. I was also able to find the
schema for the RDL, it is actually supplied by MS. I then generated the
class using xsd.exe, and it works like a charm. Thank you and also thanks
Dino for the pointers.

Mike


[quoted text, click to view]
AddThis Social Bookmark Button