XmlReader在&符号实体之后切断空格? [英] XmlReader chopping off whitespace after ampersand entity?

查看:253
本文介绍了XmlReader在&符号实体之后切断空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是奇怪的。我有一个WCF Message 和I '试图将身体的内容读入XmlDocument。消息体的内容看起来像这样在线上(当WCF跟踪打开时检查):

 < abc> 
< timeZone>(GMT-05:00)东部时间(美国&加拿大)< / timeZone>
< / abc>

读者的代码如下所示:

  XmlReaderSettings settings = new XmlReaderSettings(); 
settings.IgnoreWhitespace = false;
settings.CheckCharacters = false;
XmlReader bodyReader = XmlReader.Create(
message.GetReaderAtBodyContents()。ReadSubtree(),settings);
XmlDocument messageDoc = new XmlDocument();
messageDoc.Load(bodyReader);

messageDoc 中生成的XML看起来像这样:

 < abc> 
< timeZone>(GMT-05:00)东部时间(美国和加拿大)< / timeZone>
< / abc>

那么在原来的& amp; amp; code> go?

解决方案

您可以通过删除 XmlReader 。然后在 XmlDocument 上设置PreserveWhiteSpace。您可以用以下代码替换所有代码:

  XmlDocument messageDoc = new XmlDocument(){PreserveWhitespace = true}; 
messageDoc.Load(message.GetReaderAtBodyContents()。ReadSubtree());


This is strange. I've got a WCF Message and I'm trying to read the contents of the body into an XmlDocument. The contents of the message body look like this on the wire (when inspected with WCF tracing turned on):

<abc>
    <timeZone>(GMT-05:00) Eastern Time (US &amp; Canada)</timeZone>
</abc>

The code for the reader looks like this:

XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = false;
settings.CheckCharacters = false;
XmlReader bodyReader = XmlReader.Create(
        message.GetReaderAtBodyContents().ReadSubtree(), settings);
XmlDocument messageDoc = new XmlDocument();
messageDoc.Load(bodyReader);

The resulting XML in messageDoc looks like this:

<abc>
    <timeZone>(GMT-05:00) Eastern Time (US &Canada)</timeZone>
</abc>

So where did the extra whitespace after the original &amp; go?

解决方案

You can simplify the code by removing the XmlReader. Then set the PreserveWhiteSpace on the XmlDocument. You can replace all of your code with:

XmlDocument messageDoc = new XmlDocument() { PreserveWhitespace = true };
messageDoc.Load(message.GetReaderAtBodyContents().ReadSubtree());

这篇关于XmlReader在&符号实体之后切断空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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