在C#中,如何获取空白的XML节点值?

发布于 2024-08-16 04:36:21 字数 247 浏览 4 评论 0原文

我有一个 XML 节点,其值为空格。示例:

<sampleNode> </sampleNode>

我正在使用 Serializer 从 XML 文档获取数据并将其存储在对象中。现在,我面临的问题是:如果 XML 节点值只包含空格(如上面的示例节点所示),则序列化程序会将其解释为 string.Empty。

我怎样才能克服这个问题?我需要获得实际的空白,即“”。非常感谢!

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

<sampleNode> </sampleNode>

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提赋 2024-08-23 04:36:21

假设您使用的是 XmlDocument,则应设置 PreserveWhiteSpace 属性为 True。

如果使用 XmlReader,请设置 WhitespaceHandling 属性 WhitespaceHandling 。全部。

请参阅这篇有关在序列化时保留空白的 MSDN 文章。

不同的序列化程序以不同的方式处理此问题,请尝试使用 XmlTextReader 为此,按照 论坛帖子。

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

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

See this MSDN article about Preserving White Space While Serializing.

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

浪推晚风 2024-08-23 04:36:21

示例类:

using System;

namespace GeneralTesting
{
    [Serializable]
    public class SampleNode
    {
        public string sampleNode = " ";
    }
}

示例程序:

    XmlSerializer xmls = new XmlSerializer(typeof(SampleNode));
    SampleNode sn = new SampleNode();
    using (FileStream fs = File.Open(@"C:\test.xml", FileMode.Create))
    {
        xmls.Serialize(fs, sn);
    }
    using (FileStream fs = File.OpenRead(@"C:\test.xml"))
    {
        XmlReaderSettings xmlrs = new XmlReaderSettings();
        xmlrs.IgnoreWhitespace = false;
        using (XmlReader xmlr = XmlReader.Create(fs, xmlrs))
        {
            Console.WriteLine("!{0}!", ((SampleNode) xmls.Deserialize(xmlr)).sampleNode); //output: ! !
        }
    }

Sample class:

using System;

namespace GeneralTesting
{
    [Serializable]
    public class SampleNode
    {
        public string sampleNode = " ";
    }
}

And sample program:

    XmlSerializer xmls = new XmlSerializer(typeof(SampleNode));
    SampleNode sn = new SampleNode();
    using (FileStream fs = File.Open(@"C:\test.xml", FileMode.Create))
    {
        xmls.Serialize(fs, sn);
    }
    using (FileStream fs = File.OpenRead(@"C:\test.xml"))
    {
        XmlReaderSettings xmlrs = new XmlReaderSettings();
        xmlrs.IgnoreWhitespace = false;
        using (XmlReader xmlr = XmlReader.Create(fs, xmlrs))
        {
            Console.WriteLine("!{0}!", ((SampleNode) xmls.Deserialize(xmlr)).sampleNode); //output: ! !
        }
    }
橙味迷妹 2024-08-23 04:36:21

您可以使用 CDATA 占位符以避免删除空格:

<sampleNode><![CDATA[ ]]></sampleNode>

You can use a CDATA placeholder in order to avoid the removal of the space:

<sampleNode><![CDATA[ ]]></sampleNode>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文