如何让XDocument.Load preserve换行符 [英] How to let XDocument.Load preserve line break

查看:319
本文介绍了如何让XDocument.Load preserve换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using (System.IO.StreamReader sr = new System.IO.StreamReader(path, System.Text.Encoding.GetEncoding("Windows-1252"), true))
{ 

    xdoc = XDocument.Load(sr);
}

下面是我的XML

<sheet name="sheet1" bounds="160,128,464,288">
    <text name="text1" bounds="160,128,464,288" text="a

    b"/>
</sheet>

XDocument.Load转换

XDocument.Load converts

a

b

a b

如何preserve我换行?

How to preserve my line breaks?

推荐答案

空白字符在属性由默认规范化转换为空格。这是安全得多文本中的元素,而不是属性的新生产线。

Whitespaces in attributes are normalized by default to be converted to spaces. It is much safer to have text with new lines in elements instead of attributes.

如果这是你的控制 - 设置的XmlTextReader .Normalization 应该prevent默认行为。

If it is out of your control - setting XmlTextReader.Normalization to false should prevent default behavior.

从下面的文章的部分样本:

Partial sample from the article below:

// Create the XML fragment to be parsed. 
string xmlFrag  = 
@"<item attr1='  test A B C
    1 2 3'/>
  <item attr2='&#01;'/>";                         
XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

// Show attribute value normalization.
reader.Read();
reader.Normalization = false;
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));
reader.Normalization = true;
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));

这篇关于如何让XDocument.Load preserve换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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