在C#中,如何让XML节点值是空白? [英] In C#, how to get XML node value that is white space?

查看:991
本文介绍了在C#中,如何让XML节点值是空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值,它是一种白色空间XML节点。例如:

I have a XML node with a value which is a white space. Example:

<sampleNode> </sampleNode>



我使用的是串行摆脱XML文档中的数据存储在一个对象。现在,我面临的问题是:如果XML节点值包含只是一个空格,象上面的例子节点,串行interpretates它作为一个的String.Empty。

I am using a Serializer to get the data from XML document to store it in an object. Now, the problem I am facing is: If the XML node value contains nothing but a white space, as does the sample node above, the serializer interpretates it as a string.Empty.

我怎样才能解决这个?我需要得到实际的空白,即。 !多谢

How can I overcome this? I need get the actual white space, i.e. " ". Thanks a bunch!

推荐答案

假设你正在使用XmlDocument的,你应该设置的PreserveWhiteSpace 属性为True。

Assuming you are using XmlDocument, you should set the PreserveWhiteSpace property to True.

如果使用和设置的XmlReader WhitespaceHandling 财产WhitespaceHandling.All。

If using and XmlReader set the WhitespaceHandling property WhitespaceHandling.All.

请参阅此了解保留空白虽然序列化MSDN文章。

See this MSDN article about Preserving White Space While Serializing.

不同的串行处理这种不同的方式,请尝试使用的 XmlTextReader的这一点,按的这个论坛的帖子。

The different serializers handle this different ways, try using the XmlTextReader for this, as per this forum post.

这篇关于在C#中,如何让XML节点值是空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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