XMLSerializer不能正确处理混合内容 [英] XMLSerializer don't handle mixed contents properly

查看:78
本文介绍了XMLSerializer不能正确处理混合内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

混合内容模型允许XML文档中的文本和元素的组合。


我们使用.net xmlserializer来序列化/反序列化XML。 XmlSerializer不能正确处理混合内容。


XmlSerializer处理混合内容模型到一定程度。 XmlSerializer在xml文档中添加文本,但文本的位置并不完全在原始XML文档中出现的位置。我们举一个例子


这里是原始的xml


< employee xmlns =" http://example.org/xmlserializer ">

 这里有一些文字......

  < id> 333-33-3333< / id>

 这里还有一些......

  < name> Bob Smith< / name>

 这里还有更多......

< / employee>


我们使用xmlserializer对xml进行反序列化,然后将其序列化为xml。现在xml文档更改如下


< employee xmlns =" http://example.org/xmlserializer ">&NBSP;


  < id> 333-33-3333< / id>

  < name> Bob Smith< / name>

 这里有一些文字......

 这里还有一些...... 

 这里还有更多......

< / employee>


你看到文字的位置发生了变化。


<我已经看到Microsoft代码通过反射器序列化/反序列化xml。他们将文本放在一个字符串数组中,但不保存位置。


有没有解决方案或解决方法?


我不想解析xml由xmlReader。我想修复用于序列化/反序列化的Microsoft生成的类。


谢谢。

解决方案

这个问题并不新鲜......请参阅http://msdn.microsoft.com/en-us/magazine/cc164135.aspx(页面上的第二个问题)。


简而言之,如果你想使用混合内容并保留松散文本的位置,你必须自己使用XmlReader或其他方法。


 


HTH,


ShaneB


mixed content model allows a combination of text and elements in the XML document.

we use .net xmlserializer to serialize/deserialize the XML. XmlSerializer don't handle mixed content properly.

XmlSerializer handles mixed content models to a degree. XmlSerializer add the text in the xml document, but the position of text is not exactly where they appeared in the original XML document. Let's take a example

here is original xml

<employee xmlns="http://example.org/xmlserializer">
  here is some text...
  <id>333-33-3333</id>
  here is some more...
  <name>Bob Smith</name>
  and here is even more...
</employee>

We deserialize above xml using the xmlserializer then we serialize it back into xml. Now xml document is changed as follows

<employee xmlns="http://example.org/xmlserializer"> 
  <id>333-33-3333</id>
  <name>Bob Smith</name>
  here is some text...
  here is some more... 
  and here is even more...
</employee>

You see the position of text is changed.

I have seen the Microsoft code to serialize/deserialize the xml through reflector. They put text in a string array but position is not saved.

Is there any solution or workaround?

I don't want to parse the xml by xmlReader. I want fix in the Microsoft generated classes that are used in serializaton/deserialization.

Thanks.

解决方案

This question is not new...please see http://msdn.microsoft.com/en-us/magazine/cc164135.aspx (second question on the page).

In short, if you want to use mixed content and preserve the location of loose text, you'll have to do it yourself with XmlReader or by some other method.

 

HTH,

ShaneB


这篇关于XMLSerializer不能正确处理混合内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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