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 / June 2004

Tip: Looking for answers? Try searching our database.

Using XmlRootAttribute for deserialization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Keith Patrick - 05 Jun 2004 04:47 GMT
I've seen this come up before, but in my case, things are a little more
complex, and I'm having a tough time figuring out how to set an element name
that works.  I have a configuration file that is my serialized object (I'm
using a customer deserializer to add some more XAML-like capabilities):
<AppConfigObject>
<RuntimeType:Kernel xmlns:RuntimeType="MyNamespace.DefaultKernel, Kernel"/>
</AppConfigObject>

public class AppConfigObject {
   [XmlElement]
   public Kernel {...}
}

Problem I have is deserializing the child node <Kernel> because it always
throws an error about "<Kernel xmlns='MyNamespace.DefaultKernel, Kernel'>
was not expected.".  I'm hoping that it's just something I'm missing (I've
tried several combinations of element names and defaultNamespaces) and it's
not that what I am trying is not possible.
Christoph Schittko [MVP] - 05 Jun 2004 17:29 GMT
Your XML document states that the <Kernel> element belongs to the
"MyNamespace.DefaultKernel, Kernel" namespace, but your serialization
attribute does not declare the namespace.

Try:

public class AppConfigObject {
 [XmlElement(Namespace="MyNamespace.DefaultKernel, Kernel")]
 public Kernel {...}
}

Signature

HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

> I've seen this come up before, but in my case, things are a little more
> complex, and I'm having a tough time figuring out how to set an element
[quoted text clipped - 17 lines]
> it's
> not that what I am trying is not possible.
Keith Patrick - 05 Jun 2004 21:12 GMT
The thing is, I can't set it like that in the class, because the Kernel
property is of type "BaseKernel", of which "DefaultKernel" is a subclass
(this is basically a new serializer that supports concrete implementations
of abstract properties and list items).  I tried also setting
defaultNamespace in my serializer, but the problem there is that I need to
set defautlNamespace *and* default element name in teh XmlRootAttribute,but
I can't set both without also having to provide some other params (the last
ctor for that attribute) that I cannot pass in as null, as it throws an
exception in XRA.

The way I think I am going to go is to declare my stuff like this:
<AppConfigObject>
 <Kernel>
   <MyNamespace.DefaultKernel Name="..." Attr2="..."/>
 </Kernel>
</AppConfigObject>

and just go through every assembly in the appdomain to look for
MyNamespace.DefaultKernel (it's a somewhat slow process, as it's heavy into
reflection and has to iterate through several assemblies, but this stuff is
all part of startup and not regular operation.  Another downside is that I
can't have two assemblies in the appdomain with the same namespace + class,
but I can live with that limitation until I replace the config format with
XAML snippets)

> Your XML document states that the <Kernel> element belongs to the
> "MyNamespace.DefaultKernel, Kernel" namespace, but your serialization
[quoted text clipped - 28 lines]
> > it's
> > not that what I am trying is not possible.
Keith Patrick - 06 Jun 2004 00:41 GMT
I dunno what I was thinking with that ctor stuff.  I can set the namespace
independently, and it works (at least without the Runtime: stuff):

<Kernel xmlns="MyNamespace.DefaultKernel, Kernel"/>
will deserialize just fine as long as my XmlRootAttribute has a name of
"Kernel" and a namespace of "MyNamespace.DefaultKernel, Kernel", which is
basically a non-metadata way of doing what you suggested.  Thanks for
steering me in the right direction
Christoph Schittko [MVP] - 06 Jun 2004 01:26 GMT
Would attaching multiple XmlElement attributes to the property work better
for you? Something like:

public class AppConfigObject {
 [XmlElement(typeof(BaseKernel), Namespace="MyNamespace.BaseKernel,
Kernel")]
 [XmlElement(typeof(DefaultKernel), Namespace="MyNamespace.DefaultKernel,
Kernel")]
 public Kernel {...}
}

This avoids messing with XmlRoot attributes altogether.

Signature

HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

>I dunno what I was thinking with that ctor stuff.  I can set the namespace
> independently, and it works (at least without the Runtime: stuff):
[quoted text clipped - 4 lines]
> basically a non-metadata way of doing what you suggested.  Thanks for
> steering me in the right direction
Keith Patrick - 06 Jun 2004 02:02 GMT
I'm not able to do that because I need to allow for any subclass of
BaseKernel to be used.

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.