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 转换
a
b
to
a b
如何保留换行符?
默认情况下,属性中的空格会被标准化以转换为空格。在元素中使用带有换行符的文本比在属性中使用要安全得多。
如果它超出您的控制 - 设置XmlTextReader.标准化 http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.normalization.aspx to false
应防止默认行为。
以下文章的部分示例:
// Create the XML fragment to be parsed.
string xmlFrag =
@"<item attr1=' test A B C
1 2 3'/>
<item attr2=''/>";
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"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)