473,216 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

Using XmlRootAttribute for deserialization

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.
Nov 12 '05 #1
10 6893
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 {...}
}

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

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:uK****************@TK2MSFTNGP11.phx.gbl...
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.

Nov 12 '05 #2
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 {...}
}

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

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:uK****************@TK2MSFTNGP11.phx.gbl...
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.

Nov 12 '05 #3
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)

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:O9**************@TK2MSFTNGP11.phx.gbl...
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 {...}
}

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

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:uK****************@TK2MSFTNGP11.phx.gbl...
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.


Nov 12 '05 #4
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)

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:O9**************@TK2MSFTNGP11.phx.gbl...
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 {...}
}

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

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:uK****************@TK2MSFTNGP11.phx.gbl...
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.


Nov 12 '05 #5
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
Nov 12 '05 #6
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
Nov 12 '05 #7
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.

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

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:u8****************@tk2msftngp13.phx.gbl...
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

Nov 12 '05 #8
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.

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

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:u8****************@tk2msftngp13.phx.gbl...
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

Nov 12 '05 #9
I'm not able to do that because I need to allow for any subclass of
BaseKernel to be used.
Nov 12 '05 #10
I'm not able to do that because I need to allow for any subclass of
BaseKernel to be used.
Nov 12 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JohnnySparkles | last post by:
Hi everyone, I'm currently writing an application which uses the XmlSerializer class to serialize/deserialize objects to/from xml. Now when deserializing an XmlDocument back into the object,...
0
by: Keith Patrick | last post by:
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...
0
by: Simon Gregory | last post by:
I'm trying to override the default elementnames in the (seemingly) simple case of serializing an array of GUIDs into XML. Here's the basic code I started with: Dim arrGuids(3) as Guid ...
8
by: ashoksrini | last post by:
Hi All, I have the below requirement and would like to get some feeback from the group on the best way to implement: 1. I have WSDL defined exposing few web services. 2. We dont have a...
0
by: Keith Patrick | last post by:
Could someone explain to me the ramifications of declaring XmlRoot("namespace") on a series of classes in which one or more are aggregates within another? I have a bunch of classes with a single...
0
by: Vinny_Davis | last post by:
Hello - I have a complex object which I am exposing over a webservice on our intranet. In Visual studio 2003 I had to add "" just above my complex object in Reference.CS under Reference.map...
0
by: groovyghoul | last post by:
Hi I have the following XML file: =========================================================== <?xml version="1.0" encoding="UTF-16"?> <Policy xmlns="http://tempuri.org/richard.xsd"> <TransType...
3
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
7
by: Andrew | last post by:
Hi, I am using DataContractJsonSerializer to deserialize JSON string in C# objects but I am having a problem. Suppose I have a class: class Item { public ItemId Id { get; set; }
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.