使用XmlRootAttribute进行反序列化 [英] Using XmlRootAttribute for deserialization

查看:124
本文介绍了使用XmlRootAttribute进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前看到过这个问题,但就我而言,情况稍微复杂多了,而且我很难搞清楚如何设置元素名称

有效。我有一个配置文件,它是我的序列化对象(我是

使用客户反序列化器添加更多类似XAML的功能):

< AppConfigObject>

< RuntimeType:Kernel xmlns:RuntimeType =" MyNamespace.DefaultKernel,Kernel" />

< / AppConfigObject>


public class AppConfigObject {

[XmlElement]

public Kernel {...}

}


我遇到的问题是反序列化子节点< Kernel>因为它始终是

抛出一个关于< Kernel xmlns =''MyNamespace.DefaultKernel,Kernel''>

不是预期的错误。我希望它只是我缺少的东西(我已经尝试了几个元素名称和defaultNamespaces的组合)并且它是

不是我正在尝试的东西是不可能的。

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.

推荐答案

您的XML文档声明< Kernel> element属于

" MyNamespace.DefaultKernel,Kernel"命名空间,但你的序列化

属性不会声明命名空间。


尝试:


公共类AppConfigObject {

[XmlElement(Namespace =" MyNamespace.DefaultKernel,Kernel")]

public Kernel {...}

}


-

HTH

Christoph Schittko [MVP]

软件架构师,.NET Mentor


" Keith Patrick" < RI ******************* @ nospamhotmail.com>在消息中写道

news:uK **************** @ 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...
我''我之前看到过这个问题,但在我的情况下,情况有点复杂,而且我很难搞清楚如何设置元素
名称
。我有一个配置文件,它是我的序列化对象(我是
使用客户反序列化器添加更多类似XAML的功能):
< AppConfigObject>
< RuntimeType:Kernel xmlns:RuntimeType =" MyNamespace.DefaultKernel,
Kernel"
< / AppConfigObject>
公共类AppConfigObject {
[XmlElement]
public Kernel {...}
}
我遇到的问题是反序列化子节点< Kernel>因为它总是会引发一个关于< Kernel xmlns =''MyNamespace.DefaultKernel,内核''>
的错误。我希望它只是我缺少的东西(我已经尝试了几种元素名称和defaultNamespaces的组合)并且
它不是那样的我正在尝试的是不可能的。
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.



您的XML文档声明< Kernel> element属于

" MyNamespace.DefaultKernel,Kernel"命名空间,但你的序列化

属性不会声明命名空间。


尝试:


公共类AppConfigObject {

[XmlElement(Namespace =" MyNamespace.DefaultKernel,Kernel")]

public Kernel {...}

}


-

HTH

Christoph Schittko [MVP]

软件架构师,.NET Mentor


" Keith Patrick" < RI ******************* @ nospamhotmail.com>在消息中写道

news:uK **************** @ 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...
我''我之前看到过这个问题,但在我的情况下,情况有点复杂,而且我很难搞清楚如何设置元素
名称
。我有一个配置文件,它是我的序列化对象(我是
使用客户反序列化器添加更多类似XAML的功能):
< AppConfigObject>
< RuntimeType:Kernel xmlns:RuntimeType =" MyNamespace.DefaultKernel,
Kernel"
< / AppConfigObject>
公共类AppConfigObject {
[XmlElement]
public Kernel {...}
}
我遇到的问题是反序列化子节点< Kernel>因为它总是会引发一个关于< Kernel xmlns =''MyNamespace.DefaultKernel,内核''>
的错误。我希望它只是我缺少的东西(我已经尝试了几种元素名称和defaultNamespaces的组合)并且
它不是那样的我正在尝试的是不可能的。
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.



事情是,我不能在课堂上设置它,因为内核

(这基本上是一个支持具体实现的新序列化器

的抽象属性和列表项)。我也尝试在我的序列化程序中设置

defaultNamespace,但问题是我需要在XmlRootAttribute中设置defautlNamespace *和* default元素名称,但是

我不能同时设置两者而不必提供其他一些params(该属性的最后一个
ctor),我无法传递为null,因为它会抛出一个
异常。


我认为我要去的方式是声明我的东西:

< AppConfigObject> ;

< Kernel>

< MyNamespace.DefaultKernel Name =" ..." Attr2 =" ..." />

< / Kernel>

< / AppConfigObject>


并且只需遍历appdomain中的每个程序集来查找

MyNamespace.DefaultKernel(这是一个有点慢的过程,因为它很重要而且反映了b $ b迭代几个程序集,但是这个东西是b / b
启动的所有部分而不是常规操作。另一个缺点是我在appdomain中没有
不能有两个程序集使用相同的命名空间+类,

但我可以忍受这个限制,直到我用

XAML片段替换配置格式)


Christoph Schittko [MVP]" < CH ******************** @ austin.rr.com>写在

消息新闻:O9 ************** @ TK2MSFTNGP11.phx.gbl ...
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...
您的XML文档说明了< Kernel> element属于
MyNamespace.DefaultKernel,Kernel。命名空间,但是你的序列化
属性没有声明命名空间。

尝试:
公共类AppConfigObject {
[XmlElement(Namespace =" MyNamespace) .DefaultKernel,Kernel")]
公共内核{...}
}

-
HTH
Christoph Schittko [MVP] 软件架构师,.NET Mentor

Keith Patrick < RI ******************* @ nospamhotmail.com>在消息中写道
新闻:英国**************** @ 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...
我见过这个来了以前,但在我的情况下,事情有点复杂,而且我很难搞清楚如何设置一个有效的元素
。我有一个配置文件,它是我的序列化对象
(我正在使用客户反序列化器来添加更多类似XAML的功能):
< AppConfigObject>
< RuntimeType:Kernel xmlns:RuntimeType =" MyNamespace.DefaultKernel,
Kernel"
< / AppConfigObject>
公共类AppConfigObject {
[XmlElement]
public Kernel {...}
}
我遇到的问题是反序列化子节点< Kernel>因为它
总是抛出一个关于< Kernel xmlns =''MyNamespace.DefaultKernel,
Kernel''>的错误没想到。我希望它只是我缺少的东西
(我已经尝试了几种元素名称和defaultNamespaces的组合)和
它不是那样的我正在尝试的是不可能的。
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.




这篇关于使用XmlRootAttribute进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆