XmlTextWriter使用C# [英] XmlTextWriter using C#
问题描述
亲爱的朋友,
我正在编写一个函数,该函数创建一个名为doc的xml文档,该文档将被另一个函数使用.当我使用
将创建的XML文件(名为doc)的内容写入文件时
< pre> XmlDocument doc =新的XmlDocument();
..........
..........
..........
XmlTextWriter wrtr =新的XmlTextWriter("\\ doc.xml",Encoding.Unicode);
doc.WriteTo(wrtr);
wrtr.Close();</pre>
它运作完美.但是我不想将XML文档写入文件中,而是将其内容转换为字符串并将其直接传递给使用函数,所以我使用了
< pre> .............
.............
StringWriter sw = new StringWriter();
XmlTextWriter xw =新的XmlTextWriter(sw);
doc.WriteTo(xw);
return sw.ToString();</pre>
在这种情况下,输出字符串将仅包含1024个字符(包括空格),因此它将剪切其某些内容.
Dear friends,
I am writing a function that creates an xml document named doc which will be consumed by another function. When I write the content of the XML file created (named doc) to a file using
<pre>XmlDocument doc = new XmlDocument();
..........
..........
..........
XmlTextWriter wrtr = new XmlTextWriter("\\doc.xml",Encoding.Unicode);
doc.WriteTo(wrtr);
wrtr.Close();</pre>
it works perfect. But instead of writing the XML document to a file I want to convert its content to string and pass it directly to the consuming function, so I used
<pre> .............
.............
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
doc.WriteTo(xw);
return sw.ToString();</pre>
in this case the output string will contain only 1024 characters (including space) and as a result it will cut some of its contents. What do you suggest to solve it, Thanks!
推荐答案
我认为您错过了 sw.Flush();
语句.
使用stringbuilder并在每个节点上附加xw ..将确保数据不会截断.可以使用xml的替代方法吗?像json? json只是一个字符串..
use stringbuilder and append xw on per node.. would make sure the data doesnt truncate. can you use alternatives to xml? like json? json is just a string..
这篇关于XmlTextWriter使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!