UTF - 8的大写? [英] utf-8 in uppercase?
本文介绍了UTF - 8的大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b $这是我想做的更改的一个更改,我想知道如何使用UTF-8大写而不是utf-8生成生成的xml文件? b
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
settings.IndentChars =\t;
XmlWriter writeXML = XmlWriter.Create(test_file.xml,settings);
writeXML.WriteStartDocument(false);
writeXML.WriteComment(fileLicense);
writeXML.WriteStartElement(templates);
writeXML.WriteAttributeString(xmlns,xsi,null,http://www.w3.org/2001/XMLSchema-instance);
writeXML.WriteAttributeString(xsi,noNamespaceSchemaLocation,null,test_file.xsd);
writeXML.WriteEndElement();
writeXML.WriteEndDocument();
writeXML.Close();
解决方案
我发现这个博客文章。看来这是你想要的。
public class UpperCaseUTF8Encoding:UTF8Encoding
{
//博客http://www.distribucon.com/blog/CategoryView,category,XML.aspx
//
// Dan Miser - 来自Dan Miser的想法
// Tuesday,January 29, 2008
//他使用反射器来理解编码类
//
的层次结构。返回Reflector,我注意到Encoding.WebName是用于
//写出编码字符串。我现在创建一个UTF8Encoding的后代类。
//这个类列在下面。现在,我只是调用XmlTextWriter,为编码类型传递
// UpperCaseUTF8Encoding.UpperCaseUTF8,并且一切正常工作
//。 - Dan Miser
public override string WebName
{
get {return base.WebName.ToUpper(); }
}
public static UpperCaseUTF8Encoding UpperCaseUTF8
{
get
{
if(upperCaseUtf8Encoding == null){
upperCaseUtf8Encoding = new UpperCaseUTF8Encoding();
}
返回upperCaseUtf8Encoding;
}
}
private static UpperCaseUTF8Encoding upperCaseUtf8Encoding = null;
$ b要使用此自定义编码,您需要使用XMLTextWriter作为目标XDocument保存方法。
//此部分未显示在博客
var xDoc = XDocument中。负载(xmlDocNm); //这是你的xml路径值
//在这里改变XML文档
// .Net用小写字母utf-8写XML声明。
//<?xml version =1.0encoding =utf-8?>
//这不是重头戏的问题,但NiceForm期望的是大写。
//<?xml version =1.0encoding =UTF-8?>
//我们使用一个带有自定义编码的XMLWriter来设置UTF
//设置各种选项来检索所需的输出
var settings = new XmlWriterSettings {
编码=新的UpperCaseUTF8Encoding(),//这个例子的键设置选项
NewLineHandling = System.Xml.NewLineHandling.Replace,
NewLineOnAttributes = true,
Indent = true / /为每个元素生成新行
};
using(var xmlWriter = XmlTextWriter.Create(xmlDocNm,settings)){
xDoc.Save(xmlWriter);
}
This is more of a cosmetic change that I wanted to make and I was wondering how could I make the generated xml file with UTF-8 uppercase instead of utf-8 lowercase ?
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
settings.IndentChars = "\t";
XmlWriter writeXML = XmlWriter.Create("test_file.xml", settings);
writeXML.WriteStartDocument(false);
writeXML.WriteComment(fileLicense);
writeXML.WriteStartElement("templates");
writeXML.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writeXML.WriteAttributeString("xsi", "noNamespaceSchemaLocation", null, "test_file.xsd");
writeXML.WriteEndElement();
writeXML.WriteEndDocument();
writeXML.Close();
解决方案 I found this blog post. Seems it is what you want.
public class UpperCaseUTF8Encoding : UTF8Encoding
{
// Code from a blog http://www.distribucon.com/blog/CategoryView,category,XML.aspx
//
// Dan Miser - Thoughts from Dan Miser
// Tuesday, January 29, 2008
// He used the Reflector to understand the heirarchy of the encoding class
//
// Back to Reflector, and I notice that the Encoding.WebName is the property used to
// write out the encoding string. I now create a descendant class of UTF8Encoding.
// The class is listed below. Now I just call XmlTextWriter, passing in
// UpperCaseUTF8Encoding.UpperCaseUTF8 for the Encoding type, and everything works
// perfectly. - Dan Miser
public override string WebName
{
get { return base.WebName.ToUpper(); }
}
public static UpperCaseUTF8Encoding UpperCaseUTF8
{
get
{
if (upperCaseUtf8Encoding == null) {
upperCaseUtf8Encoding = new UpperCaseUTF8Encoding();
}
return upperCaseUtf8Encoding;
}
}
private static UpperCaseUTF8Encoding upperCaseUtf8Encoding = null;
}
To use this custom encoding you need to use an XMLTextWriter as the destination to the XDocument Save method.
// This section not shown in the blog
var xDoc = XDocument.Load(xmlDocNm); //This is your xml path value
// Changes to XML Document here
// .Net writes the XML declaration using lower case utf-8.
// <?xml version="1.0" encoding="utf-8"?>
// It is not suppesed to matter but NiceForm expects the delcaration to be uppercase.
// <?xml version="1.0" encoding="UTF-8"?>
// We are using a XMLWriter with a custom Encoding to captialize the UTF
// Set various options to retrive the desired output
var settings = new XmlWriterSettings {
Encoding = new UpperCaseUTF8Encoding(), // Key setting option for this example
NewLineHandling = System.Xml.NewLineHandling.Replace,
NewLineOnAttributes = true,
Indent = true // Generate new lines for each element
};
using (var xmlWriter =XmlTextWriter.Create(xmlDocNm, settings)) {
xDoc.Save(xmlWriter);
}
这篇关于UTF - 8的大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文