XML反序列化期间的System.NullReferenceException [英] System.NullReferenceException during XML Deserialization

查看:95
本文介绍了XML反序列化期间的System.NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了XMLSerializer的奇怪行为。我是

反序列化XmlNode。 XmlNode作为调用

IConfigurationSectionHandler.Create的一部分传递。我的XML看起来像:


< sectionName>

< settings>

...

...

...

< / settings>

< / sectionName>


我已经定义了我的课程:


[XmlRoot(Namespace ="",IsNullable = false,ElementName =" settings)]

公共类MyAppSettings

{

....

}


我我正在将我的XmlNode传递给XmlSerializer:


返回serializer.Deserialize(new XmlNodeReader(section.ChildNodes [0]));


发生的事情是我从

System.Xml.Serialization.XmlSerializationReader.Un knownNode()例程中获取了一个System.NullReferenceException,该例程由动态创建的
调用派生XmlSerializationReader

代码。我可以在堆栈跟踪中看到这一点。


也许有人可以对此有所了解吗?



-

-


David B. Bitton
da *** @ codenoevil.com
www.codenoevil.com


Code Made Fresh DailyT

I''m experiencing odd behavior on the part of the XMLSerializer. I''m
deserializing an XmlNode. The XmlNode is pass as part of a call to
IConfigurationSectionHandler.Create. My XML looks like:

<sectionName>
<settings>
...
...
...
</settings>
</sectionName>

I have define my class as such:

[XmlRoot(Namespace="", IsNullable=false, ElementName="settings")]
public class MyAppSettings
{
....
}

I am passing my XmlNode to the XmlSerializer like so:

return serializer.Deserialize( new XmlNodeReader( section.ChildNodes[0] ) );

What happens is I get an System.NullReferenceException eminating from the
System.Xml.Serialization.XmlSerializationReader.Un knownNode() routine which
is being called by the dynamically created derivative XmlSerializationReader
code. I can see this in the stack trace.

Perhaps someone can shed some light on this?


--
--

David B. Bitton
da***@codenoevil.com
www.codenoevil.com

Code Made Fresh DailyT

推荐答案

大卫,


你能带走吗?看看我在MSDN上的故障排除文章,让我知道

是否有助于诊断问题?

-

HTH

Christoph Schittko [MVP]

软件架构师,.NET Mentor


[0]
http://msdn.microsoft.com /xml/defaul...trblshtxsd.asp

" David B. Bitton" <哒*** @ codenoevil.com>在消息中写道

新闻:O7 ************** @ tk2msftngp13.phx.gbl ...
David,

Can you take a look at my troubleshooting article on MSDN and let me know if
that helped diagnosing the problem?
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[0]
http://msdn.microsoft.com/xml/defaul...trblshtxsd.asp
"David B. Bitton" <da***@codenoevil.com> wrote in message
news:O7**************@tk2msftngp13.phx.gbl...
我正在体验XMLSerializer方面的奇怪行为。我正在反序列化XmlNode。 XmlNode作为调用
IConfigurationSectionHandler.Create的一部分传递。我的XML看起来像:

< sectionName>
< settings>
...
...
...
< / settings>
< / sectionName>

我已经定义了我的类:

[XmlRoot(Namespace ="",IsNullable = false,ElementName =" settings")]
公共类MyAppSettings
{
...

我将我的XmlNode传递给XmlSerializer像这样:

返回serializer.Deserialize(新的XmlNodeReader(
section.ChildNodes [0]));

我会得到一个System.NullReferenceException eminating来自
System.Xml.Serialization.XmlSerializationReader.Un knownNode()例程
由动态创建的派生XmlSerializationReader
代码调用。我可以在堆栈跟踪中看到这一点。

也许有人可以对此有所了解吗?


-
-

David B. Bitton
da***@codenoevil.com
www.codenoevil.com

Code Made Fresh DailyT
I''m experiencing odd behavior on the part of the XMLSerializer. I''m
deserializing an XmlNode. The XmlNode is pass as part of a call to
IConfigurationSectionHandler.Create. My XML looks like:

<sectionName>
<settings>
...
...
...
</settings>
</sectionName>

I have define my class as such:

[XmlRoot(Namespace="", IsNullable=false, ElementName="settings")]
public class MyAppSettings
{
...
}

I am passing my XmlNode to the XmlSerializer like so:

return serializer.Deserialize( new XmlNodeReader(
section.ChildNodes[0] ) );

What happens is I get an System.NullReferenceException eminating from the
System.Xml.Serialization.XmlSerializationReader.Un knownNode() routine
which
is being called by the dynamically created derivative
XmlSerializationReader
code. I can see this in the stack trace.

Perhaps someone can shed some light on this?


--
--

David B. Bitton
da***@codenoevil.com
www.codenoevil.com

Code Made Fresh DailyT



大卫,


你能看看我在MSDN上的故障排除文章,让我知道是否

有助于诊断问题吗?

-

HTH

Christoph Schittko [MVP]

软件架构师,.NET Mentor


[0]
http://msdn.microsoft.com/xml/defaul...trblshtxsd.asp

" David B. Bitton" <哒*** @ codenoevil.com>在消息中写道

新闻:O7 ************** @ tk2msftngp13.phx.gbl ...
David,

Can you take a look at my troubleshooting article on MSDN and let me know if
that helped diagnosing the problem?
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[0]
http://msdn.microsoft.com/xml/defaul...trblshtxsd.asp
"David B. Bitton" <da***@codenoevil.com> wrote in message
news:O7**************@tk2msftngp13.phx.gbl...
我正在体验XMLSerializer方面的奇怪行为。我正在反序列化XmlNode。 XmlNode作为调用
IConfigurationSectionHandler.Create的一部分传递。我的XML看起来像:

< sectionName>
< settings>
...
...
...
< / settings>
< / sectionName>

我已经定义了我的类:

[XmlRoot(Namespace ="",IsNullable = false,ElementName =" settings")]
公共类MyAppSettings
{
...

我将我的XmlNode传递给XmlSerializer像这样:

返回serializer.Deserialize(新的XmlNodeReader(
section.ChildNodes [0]));

我会得到一个System.NullReferenceException eminating来自
System.Xml.Serialization.XmlSerializationReader.Un knownNode()例程
由动态创建的派生XmlSerializationReader
代码调用。我可以在堆栈跟踪中看到这一点。

也许有人可以对此有所了解吗?


-
-

David B. Bitton
da***@codenoevil.com
www.codenoevil.com

Code Made Fresh DailyT
I''m experiencing odd behavior on the part of the XMLSerializer. I''m
deserializing an XmlNode. The XmlNode is pass as part of a call to
IConfigurationSectionHandler.Create. My XML looks like:

<sectionName>
<settings>
...
...
...
</settings>
</sectionName>

I have define my class as such:

[XmlRoot(Namespace="", IsNullable=false, ElementName="settings")]
public class MyAppSettings
{
...
}

I am passing my XmlNode to the XmlSerializer like so:

return serializer.Deserialize( new XmlNodeReader(
section.ChildNodes[0] ) );

What happens is I get an System.NullReferenceException eminating from the
System.Xml.Serialization.XmlSerializationReader.Un knownNode() routine
which
is being called by the dynamically created derivative
XmlSerializationReader
code. I can see this in the stack trace.

Perhaps someone can shed some light on this?


--
--

David B. Bitton
da***@codenoevil.com
www.codenoevil.com

Code Made Fresh DailyT



Chris,

我会看看这个网站,但在此之前,让我告诉你一下

我为了让它发挥作用而做了什么。一旦我删除了

XML中的所有空格,就可以了。基本上,我将其更改为:


< sectionName>< settings> ...< / settings>< / sectionName>


并且神奇地开始起作用了。我注意到的一件事是,当我在cmd窗口的XmlNodeReader中查看

时,在我调用Read()一次之后,

值是\ tt \\t\r\\\
\"这显然是一个空白节点。看起来好像动态代码试图解释的是




-

-


David B. Bitton
da***@codenoevil.com
www.codenoevil.com


Code Made Fresh Daily?

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

消息新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Chris,
I''ll take a look at the site, but before I do, let me tell you about
what I did to get it to work. Once I removed all of the whitespace in the
XML, then it was OK. Basically, i changed it to:

<sectionName><settings>...</settings></sectionName>

and magically it started to work. One thing I noticed is that when I looked
at the XmlNodeReader in the cmd window, after I called Read() once, the
value was "\t\t\r\n\" which obviously is a whitespace node. Looks like that
was what the dynamic code was attempting to interpret.

--
--

David B. Bitton
da***@codenoevil.com
www.codenoevil.com

Code Made Fresh Daily?
"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
David,

你能看看我在MSDN上的故障排除文章,并告诉我
,如果这有助于诊断问题吗?

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

[0]
http://msdn.microsoft.com/xml/defaul...trblshtxsd。 asp

David B. Bitton <哒*** @ codenoevil.com>在消息中写道
新闻:O7 ************** @ tk2msftngp13.phx.gbl ...
David,

Can you take a look at my troubleshooting article on MSDN and let me know if that helped diagnosing the problem?
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[0]
http://msdn.microsoft.com/xml/defaul...trblshtxsd.asp

"David B. Bitton" <da***@codenoevil.com> wrote in message
news:O7**************@tk2msftngp13.phx.gbl...
我正在经历奇怪的行为XMLSerializer的一部分。我正在反序列化XmlNode。 XmlNode作为调用
IConfigurationSectionHandler.Create的一部分传递。我的XML看起来像:

< sectionName>
< settings>
...
...
...
< / settings>
< / sectionName>

我已经定义了我的类:

[XmlRoot(Namespace ="",IsNullable = false,ElementName =" settings")]
公共类MyAppSettings
{
...

我将我的XmlNode传递给XmlSerializer像这样:

返回serializer.Deserialize(新的XmlNodeReader(
section.ChildNodes [0]));

我会得到一个System.NullReferenceException eminating来自
的System.Xml.Serialization.XmlSerializationReader.Un knownNode()例程
由动态创建的派生XmlSerializationReader
代码调用。我可以在堆栈跟踪中看到这一点。

也许有人可以对此有所了解吗?


-
-

David B. Bitton
da***@codenoevil.com
www.codenoevil.com

Code Made Fresh DailyT
I''m experiencing odd behavior on the part of the XMLSerializer. I''m
deserializing an XmlNode. The XmlNode is pass as part of a call to
IConfigurationSectionHandler.Create. My XML looks like:

<sectionName>
<settings>
...
...
...
</settings>
</sectionName>

I have define my class as such:

[XmlRoot(Namespace="", IsNullable=false, ElementName="settings")]
public class MyAppSettings
{
...
}

I am passing my XmlNode to the XmlSerializer like so:

return serializer.Deserialize( new XmlNodeReader(
section.ChildNodes[0] ) );

What happens is I get an System.NullReferenceException eminating from the System.Xml.Serialization.XmlSerializationReader.Un knownNode() routine
which
is being called by the dynamically created derivative
XmlSerializationReader
code. I can see this in the stack trace.

Perhaps someone can shed some light on this?


--
--

David B. Bitton
da***@codenoevil.com
www.codenoevil.com

Code Made Fresh DailyT




这篇关于XML反序列化期间的System.NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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