力的XDocument写入字符串使用UTF-8编码 [英] Force XDocument to write to String with UTF-8 encoding
问题描述
我希望能够编写XML与申报和使用UTF-8编码的字符串。这似乎是非常困难的完成。
我已经阅读了一下周围,并试图一些流行的答案,这个的,但他们都有问题。我当前的代码正确输出为UTF-8,但不维护的XDocument的原始格式(即缩进/空格)!
任何人都可以提供一些建议吗?
的XDocument XML =新的XDocument(新XDeclaration(1.0,UTF-8,是),xelementXML);
的MemoryStream毫秒=新的MemoryStream();使用
(XmlWriter的XW =新的XmlTextWriter(MS,Encoding.UTF8))
{
xml.Save(XW);
xw.Flush();
StreamReader的SR =新的StreamReader(毫秒);
ms.Seek(0,SeekOrigin.Begin);
字符串的xmlString = sr.ReadToEnd();
}
的
XML要求的格式是相同的方式的ToString()
将其格式化即
<?XML版本=1.0编码=UTF-8独立=YES>?;
<根和GT;
<节点>等等< /节点>
< /根>
什么我目前看到的是
<?XML版本=1.0编码=UTF-8独立=YES><根><节点>等等< /节点>< /根>
更新
我设法得到这个由工作加入的XmlTextWriter
设置...似乎很笨重,但!
的MemoryStream MS =新的MemoryStream();
XmlWriterSettings设置=新XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Indent = TRUE;使用(XmlWriter的XW = XmlTextWriter.Create(MS,设置))
{
xml.Save(XW)
;
xw.Flush();
StreamReader的SR =新的StreamReader(毫秒);
ms.Seek(0,SeekOrigin.Begin);
串等等= sr.ReadToEnd();
}
试试这个:
使用系统;
:使用System.IO;
使用System.Text;
使用System.Xml.Linq的;
类测试
{
静态无效的主要()
{
的XDocument DOC = XDocument.Load(的test.xml,
LoadOptions.PreserveWhitespace);
doc.Declaration =新XDeclaration(1.0,UTF-8,NULL);
StringWriter的作家=新Utf8StringWriter();
doc.Save(作家,SaveOptions.None);
Console.WriteLine(作家);
}
私有类Utf8StringWriter:StringWriter的
{
公众覆盖编码编码{{返回Encoding.UTF8; }}
}
}
当然,你还没有告诉我们你是如何构建文档,这使得它很难对其进行测试...我只是试着用一只手构造的XDocument
,并包含相关空白了。
I want to be able to write XML to a String with the declaration and with UTF-8 encoding. This seems mighty tricky to accomplish.
I have read around a bit and tried some of the popular answers for this but the they all have issues. My current code correctly outputs as UTF-8 but does not maintain the original formatting of the XDocument (i.e. indents / whitespace)!
Can anyone offer some advice please?
XDocument xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), xelementXML);
MemoryStream ms = new MemoryStream();
using (XmlWriter xw = new XmlTextWriter(ms, Encoding.UTF8))
{
xml.Save(xw);
xw.Flush();
StreamReader sr = new StreamReader(ms);
ms.Seek(0, SeekOrigin.Begin);
String xmlString = sr.ReadToEnd();
}
The XML requires the formatting to be identical to the way .ToString()
would format it i.e.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
<node>blah</node>
</root>
What I'm currently seeing is
<?xml version="1.0" encoding="utf-8" standalone="yes"?><root><node>blah</node></root>
Update
I have managed to get this to work by adding XmlTextWriter
settings... It seems VERY clunky though!
MemoryStream ms = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Indent = true;
using (XmlWriter xw = XmlTextWriter.Create(ms, settings))
{
xml.Save(xw);
xw.Flush();
StreamReader sr = new StreamReader(ms);
ms.Seek(0, SeekOrigin.Begin);
String blah = sr.ReadToEnd();
}
Try this:
using System;
using System.IO;
using System.Text;
using System.Xml.Linq;
class Test
{
static void Main()
{
XDocument doc = XDocument.Load("test.xml",
LoadOptions.PreserveWhitespace);
doc.Declaration = new XDeclaration("1.0", "utf-8", null);
StringWriter writer = new Utf8StringWriter();
doc.Save(writer, SaveOptions.None);
Console.WriteLine(writer);
}
private class Utf8StringWriter : StringWriter
{
public override Encoding Encoding { get { return Encoding.UTF8; } }
}
}
Of course, you haven't shown us how you're building the document, which makes it hard to test... I've just tried with a hand-constructed XDocument
and that contains the relevant whitespace too.
这篇关于力的XDocument写入字符串使用UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!