Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / December 2004

Tip: Looking for answers? Try searching our database.

Deserialization issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike Sarbu - 22 Dec 2004 03:58 GMT
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] - 23 Dec 2004 16:51 GMT
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

> Hello all,
>
[quoted text clipped - 52 lines]
>
> Mike
Christoph Schittko [MVP] - 23 Dec 2004 19:23 GMT
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/

> -----Original Message-----
> From: Dino Chiesa [Microsoft] [mailto:dinoch@online.microsoft.com]
[quoted text clipped - 33 lines]
>
> C. the class I got by following the steps above was this:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.abcinc.
co
> m/objectdefinition")]

[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.abcinc.
co
> m/objectdefinition",
> IsNullable=false)]
[quoted text clipped - 76 lines]
> >
> > Mike
Mike Sarbu - 28 Dec 2004 03:46 GMT
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

> Mike,
> to solve these sorts of problems, you can try working in the opposite
[quoted text clipped - 105 lines]
>>
>> Mike
Christoph Schittko [MVP] - 28 Dec 2004 04:15 GMT
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

> -----Original Message-----
> From: Mike Sarbu [mailto:m_sarbu@yahoo.com]
[quoted text clipped - 68 lines]
> >
> > C. the class I got by following the steps above was this:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.abcinc.
co
> m/objectdefinition")]

[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.abcinc.
co
> m/objectdefinition",
> > IsNullable=false)]
[quoted text clipped - 76 lines]
> >>
> >> Mike

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.