如何从C#LINQ解析XML时,我preserve空白字符 [英] How do I preserve whitespace characters when parsing XML from C# LINQ

查看:205
本文介绍了如何从C#LINQ解析XML时,我preserve空白字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么我需要做的,无论是我的C#code或我的XML文档,这样的的XDocument解析器读取文字空白的的S 的XElement S'

What do I need to do in either my C# code or my XML document so that the XDocument parser reads literal whitespace for Values of XElements?


背景

我有一个XML文档,其中有一部分是这样的:

I have an XML document, part of which looks like this:

    <NewLineString>&#10;&#13;</NewLineString>
    <IndentString>    </IndentString>

我添加每个的XElement 来使用LINQ查询数据字典的价值;在 .ForEach 部分看起来是这样的:

I'm adding the values of each XELement to a data dictionary using a LINQ query; the .ForEach part looks like this:

    .ForEach(x => SchemaDictionary.Add(
        LogicHelper.GetEnumValue(x.Name.ToString()), x.Value));    

要测试,看看是否空白值分别为preserved,我打印出一行数据字典中的每个值项的字符数。在下面的code, X 重presents一个 KeyValuePair <​​/ code>和总结被简单地使字符整数值的字符串:

To test to see if the whitespace values were preserved, I'm printing out a line of the character numbers of each value item in the data dictionary. In the following code, x represents a KeyValuePair and the Aggregate is simply making a string of the character integer values:

x.Value.ToCharArray()
    .Aggregate<char,string>("",(word,c) => word + ((int)c).ToString() + " " )
    ));

我期望看到 10 13 &LT; NewLineString&GT; 价值 32 32 32 32 &LT; IndentString&GT; 值。然而,什么也没有打印的每个值(注:如&放大器在XML其他逃脱值; LT; 打印自己的性格数字正确的)。

I expected to see 10 13 for the <NewLineString> value and 32 32 32 32 for the <IndentString> value. However, nothing was printed for each value (note: other escaped values in the XML such as &lt; printed their character numbers correctly).

什么我需要做的,无论是我的C#code或我的XML文档,使我的解析器增加了完整的空白字符串数据字典?

What do I need to do in either my C# code or my XML document so that my parser adds the complete whitespace string to the Data Dictionary?

推荐答案

试着使用的 LoadOptions。preserveWhitespace

这篇关于如何从C#LINQ解析XML时,我preserve空白字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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