将字符串转换为xml时,XML-Cant中的换行符保留换行符 [英] Line Break in XML-Cant preserve linebreaks when converting string to xml
问题描述
大家好
有没有办法在xml中保留换行符?
我有包含换行符和其他信息的数据(字符串).我需要保留它.但是当我转换为xml时,它丢失了所有换行符.
有什么可能的方法吗?
我正在使用"XMLDOCUMENT.loadXml"方法将字符串加载到xml."PreserveWhitespace"属性无法满足要求.
Hi all
Is there Any way to preserve line breaks in xml?
am having data(string) that contain line breaks and other informations.and i need to preserve that.but When i converted into xml it lost all line breaks.
Is there any possible way to do so?
Am using "XMLDOCUMENT.loadXml" method to load string to xml."PreserveWhitespace" property doesn''t feed the requirement.
推荐答案
是\ n吗?
Mean the \n?
XmlDocument doc = new XmlDocument();
doc.LoadXml("<xml>This my first string \\n This is my second</xml>");
MessageBox.Show(doc.OuterXml.ToString());
实际上,换行符不会丢失.在转换xml时,将其应用并打印第二行.相反,如果要显示它,则需要对其进行转义
Actually the new line chars are not lost. While converting xml it apply it and print a second line. Instead if you want to display it , then need to escape it
选中此链接.
http://msdn.microsoft.com/en-us/library/ms256097.aspx [ ^ ]
我认为您需要将换行符设置为\r\n
才能查看数据中的实际换行符.
Check this link.
http://msdn.microsoft.com/en-us/library/ms256097.aspx[^]
I think you need to set the line break to\r\n
to see an actual line break in your data.
StringBuilder outstr = new StringBuilder();
XmlWriterSettings设置=新的XmlWriterSettings();
settings.Indent = true;
settings.NewLineChars =环境.NewLine;
settings.NewLineHandling = NewLineHandling.Replace;
XmlWriter w = XmlWriter.Create(outstr,settings);
XmlDocument doc =新的XmlDocument();
doc.LoadXml(xmlString);
doc.WriteTo(w);
字符串here_is_a_formatted_xml_string = outstr.ToString();
StringBuilder outstr = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent=true;
settings.NewLineChars = Environment.NewLine;
settings.NewLineHandling = NewLineHandling.Replace;
XmlWriter w = XmlWriter.Create(outstr, settings);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
doc.WriteTo(w);
string here_is_a_formatted_xml_string = outstr.ToString();
这篇关于将字符串转换为xml时,XML-Cant中的换行符保留换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!