Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C#LINQ解析XML时如何保留空白字符_C#_.net_Xml_Linq_Whitespace - Fatal编程技术网

从C#LINQ解析XML时如何保留空白字符

从C#LINQ解析XML时如何保留空白字符,c#,.net,xml,linq,whitespace,C#,.net,Xml,Linq,Whitespace,在我的C#code或XML文档中我需要做什么,以便XDocument解析器读取XElements的Values的文本空白? 背景 我有一个XML文档,其中一部分如下所示: <NewLineString>&#10;&#13;</NewLineString> <IndentString> </IndentString> .ForEach(x => SchemaDictionary.Add(

在我的C#code或XML文档中我需要做什么,以便XDocument解析器读取
XElement
s的
Value
s的文本空白?


背景

我有一个XML文档,其中一部分如下所示:

    <NewLineString>&#10;&#13;</NewLineString>
    <IndentString>    </IndentString>
    .ForEach(x => SchemaDictionary.Add(
        LogicHelper.GetEnumValue(x.Name.ToString()), x.Value));    
为了测试是否保留了空白值,我打印出数据字典中每个值项的一行字符号。在下面的代码中,
x
表示一个
KeyValuePair
,而
Aggregate
只是生成一个字符串整数值:

x.Value.ToCharArray()
    .Aggregate<char,string>("",(word,c) => word + ((int)c).ToString() + " " )
    ));
x.Value.ToCharArray()
.Aggregate(“,(word,c)=>word+((int)c).ToString()+”)
));
我希望看到
值的
1013
值的
32
。但是,没有为每个值打印任何内容(注意:XML中的其他转义值,如
正确打印了它们的字符号)


我需要在我的C代码或XML文档中做什么,以便我的解析器将完整的空白字符串添加到数据字典中?

尝试用加载方式加载XDocument

XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = true;
    doc.Load("book.xml");

或者只需将输入xml修改为:

<NewLineString>&#10;&#13;</NewLineString>
<IndentString xml:space="preserve">    </IndentString>



如何加载XML文档?最初的问题是关于LINQ,而不是System.XML。但是关于
XmlDocument
s的回答把我和这个问题联系起来,所以这个答案还是有用的。