gpt4 book ai didi

c# - 如何让 XDocument.Load 保留换行符

转载 作者:太空狗 更新时间:2023-10-30 01:06:31 25 4
gpt4 key购买 nike

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

a b

如何保留我的换行符?

最佳答案

属性中的空格默认被规范化为空格。在元素而不是属性中使用带有换行符的文本要安全得多。

如果它超出你的控制 - 设置 XmlTextReader.Normalizationfalse 应该防止默认行为。

以下文章的部分示例:

// 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"));

关于c# - 如何让 XDocument.Load 保留换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15283585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com