从XmlWriter的编码删除 [英] Remove encoding from XmlWriter

查看:218
本文介绍了从XmlWriter的编码删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的XmlWriter和XmlWriterSettings写一个XML文件保存到磁盘。然而,这是解析XML文件系统抱怨编码



 <?XML版本=1.0编码=UTF-8>?; 



想要的东西是这样的:

 <?XML版本=1.0>?; 

如果我尝试OmitXmlDeclaration = true,则我不明白的XML行的。

 字符串destinationName =C:\\temp\\file.xml 
串strClassification =无;

XmlWriterSettings设置=新XmlWriterSettings();
settings.Indent = TRUE;使用(XmlWriter的作家= XmlWriter.Create(destinationName,设置))
{
writer.WriteStartDocument()

;
writer.WriteStartElement(ChunkData);
writer.WriteElementString(分类,strClassification);
writer.WriteEndElement();
}


解决方案

就遇到了这个 - -




  1. 删除 XmlWriterSettings()干脆使用的XmlTextWriter()的格式的缩进场。

  2. 传入编码参数> XmlTextWriter对象的构造函数



下面的代码将创建你要找的输出:
<?XML版本=1.0>

 变种W =新的XmlTextWriter(文件名,NULL); 
w.Formatting = Formatting.Indented;
w.WriteStartDocument();
w.WriteStartElement(ChunkData);
w.WriteEndDocument();
w.Close();



.Close()高效地创建文件 - 在使用创建()办法也将工作


I'm using XmlWriter and XmlWriterSettings to write an XML file to disk. However, the system that is parsing the XML file is complaining about the encoding.

<?xml version="1.0" encoding="utf-8"?>

What it wants is this:

<?xml version="1.0" ?>

If I try OmitXmlDeclaration = true, then I don't get the xml line at all.

string destinationName = "C:\\temp\\file.xml";
string strClassification = "None";

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

using (XmlWriter writer = XmlWriter.Create(destinationName, settings))
{
    writer.WriteStartDocument();
    writer.WriteStartElement("ChunkData");
    writer.WriteElementString("Classification", strClassification);
    writer.WriteEndElement();
}

解决方案

Just ran into this ---

  1. remove the XmlWriterSettings() altogether and use the XmlTextWriter()'s Formatting field for indention.
  2. pass in null for the Encoding argument of XmlTextWriter's ctor

The following code will create the output that you're looking for: <?xml version="1.0" ?>

var w = new XmlTextWriter(filename, null);
w.Formatting = Formatting.Indented; 
w.WriteStartDocument(); 
w.WriteStartElement("ChunkData");
w.WriteEndDocument(); 
w.Close();

The .Close() effectively creates the file - the using and Create() approach will also work.

这篇关于从XmlWriter的编码删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆