Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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# 在xml中保留特殊字符_C#_Xml_Linq To Xml_Carriage Return_Linefeed - Fatal编程技术网

C# 在xml中保留特殊字符

C# 在xml中保留特殊字符,c#,xml,linq-to-xml,carriage-return,linefeed,C#,Xml,Linq To Xml,Carriage Return,Linefeed,我在db表中存储了一个带有换行符的xml字符串。 在我的C#3.5程序中,我使用Linq to xml加载和操作它,然后在UI表单的textbox控件中将它显示为字符串 我需要缩进这个xml,并在UI中显示它时保留换行符/回车符 我可以缩进它,但如何在xml中保留LF/CR字符 下面是示例C#代码: XElement rootNode=CreateRootNode(); XElement testXmlNode=XElement.Parse(xmlFromDbWithLFChars); 添加(t

我在db表中存储了一个带有换行符的xml字符串。 在我的C#3.5程序中,我使用Linq to xml加载和操作它,然后在UI表单的textbox控件中将它显示为字符串

我需要缩进这个xml,并在UI中显示它时保留换行符/回车符

我可以缩进它,但如何在xml中保留LF/CR字符

下面是示例C#代码:

XElement rootNode=CreateRootNode();
XElement testXmlNode=XElement.Parse(xmlFromDbWithLFChars);
添加(testXmlNode);
var builder=新的StringBuilder();
var设置=新的XmlWriterSettings()
{
缩进=真
};
使用(var writer=XmlWriter.Create(生成器,设置))
{
rootNode.WriteTo(writer);
}
xmlString=builder.ToString();
xmlString=xmlString.Replace(
;”,Environment.NewLine)//不起作用
xmlString=xmlString.Replace(
;”,Environment.NewLine)//不起作用
//以下是xml在UI控件中的外观:

每当您将XML文档转换为字符串并开始操作字符串时,您都应该对自己说,“赛尔夫,我做错了什么。”从您的描述中,我不确定这是不是真的,但我敢打赌是真的

如果从数据库中提取的XML中的空白很重要,那么在将其解析为
XElement
时,您希望保留它。为此,请使用执行此操作的
XElement.Parse的重载,例如:

XElement testXmlNode = XElement.Parse(xmlFromDbWithLFChars, LoadOptions.PreserveWhitespace);

当您执行此操作时,解析器将在已解析的
XElement
文档的文本节点中保留空白字符,这些字符正好位于原始字符串中的位置
XmlWriter
不会弄乱文本节点中现有的空白(尽管如果您告诉它缩进,它会添加新的空白),因此这应该可以满足您的需要。

您可以使用XmlReader保留新行和所有内容。。以下是在测试时对我来说效果很好的示例代码:

System.Xml.XmlReader reader = System.Xml.XmlReader.Create("XML URI here");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while (reader.Read())
{
    sb.Append(reader.ReadOuterXml());
}
reader.Close();
txtXML.InnerText = sb.ToString();
txtXML.Visible = true;

在我的测试中,我加载了XML文件,您可以加载处理过的XML字符串。

您要做的是在XmlWriter上设置格式设置,因此更改行:

var settings = new XmlWriterSettings() 
    { 
     Indent = true 
    }; 
对这样的事情:

var settings = new XmlWriterSettings() 
    { 
     Indent = true,
     IndentChars = "\n",
     NewLineOnAttributes = true
    }; 

谢谢大家的回复。终于,我可以让它工作了


我的方法不使用Linq2Xml/SAX解析器。我使用StringBuilder生成xml,并将其显示在winforms富文本框控件的UI中。现在,我可以看到UI中的换行符。

您是否尝试确保文本框处于多行模式,并且



通过保留,您的意思是希望XML新行与UI中的新行类似?还有一个问题:当您说“/”不起作用时,您的意思是它显示两行新行,或者根本没有?Beemer,它在UI中的XML中没有显示任何新行。您能举个例子吗?例如,
xmlFromDbWithLFChars
变量+5中包含的输入值是多少?这个问题一团糟。它与C#或XML的关系似乎没有与UI控件的关系那么大。嗨,Robert,谢谢你的回答…但是这种方法对我不起作用…我仍然无法在UI的textarea中的XML中看到换行符。当你说“textarea”时,你是指HTML textarea吗?因为TEXTAREA元素中的新行由CR/LF对表示。如果XML文本中只有LF字符,它们不会在文本区域中呈现为换行符。这是.Net Winform上的textbox控件。那么这个问题与XML无关,是吗?愚蠢的问题,但文本框是否启用了多行?它接受回车吗?谢谢你的回答…我需要在这里使用XmlReaderSettings对象来保留格式吗?@user40907不,你可以按原样使用代码,只需将自己的XML加载为纯文本。@user40907你能用XML发布你如何设置文本框数据的代码吗?也许我可以复制然后解决这个问题。我只是用xml字符串设置textbox的text属性。你应该添加你用作答案的解决方案。“使用StringBuilder生成xml”-很抱歉,但这根本不可能是正确的。XML不是字符串数据,无论是正则表达式解析还是stringbuilder组装,试图将其视为字符串数据都缺乏严格性,易于维护,极易出现人为错误。Annakata,你说的是对的,但是,不幸的是,我不能给业务提供同样的理由…因为我不想走StringBuilder的路,因为我知道它是脆弱的代码,我首先尝试使用Linq2API、SAX和DOM解析器…但是cudnt没有得到所需的格式化输出。谢谢。谢谢阿尔蒙德!。在我最初的方法中,我唯一错过的就是使用“NewLineOnAttributes=true”,这似乎非常有效!!!容易错过,很高兴它有帮助!
var settings = new XmlWriterSettings() 
    { 
     Indent = true,
     IndentChars = "\n",
     NewLineOnAttributes = true
    }; 
public void CreateMyMultilineTextBox() {
   // Create an instance of a TextBox control.
   TextBox textBox1 = new TextBox();

   // Set the Multiline property to true.
   textBox1.Multiline = true;
   // Add vertical scroll bars to the TextBox control.
   textBox1.ScrollBars = ScrollBars.Vertical;
   // Allow the RETURN key to be entered in the TextBox control.
   textBox1.AcceptsReturn = true;
   // Allow the TAB key to be entered in the TextBox control.
   textBox1.AcceptsTab = true;
   // Set WordWrap to true to allow text to wrap to the next line.
   textBox1.WordWrap = true;
   // Set the default text of the control.
   textBox1.Text = "Welcome!";
 }