c# - 大写的UTF-8?

标签 c# xml formatting

这更像是我想做的一个装饰性更改,我想知道如何使生成的 xml 文件使用 UTF-8 大写而不是 utf-8 小写?

        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();

最佳答案

我找到了 this blog post .看来这就是你想要的。

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;
}

要使用此自定义编码,您需要使用 XMLTextWriter 作为 XDocument Save 方法的目标。

// 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);
}

关于c# - 大写的UTF-8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4291332/

相关文章:

java - 如何检查 XML 文件是否具有最小结构(Java)?

iphone - AudioConverterConvertBuffer 问题与 insz 错误

c# - 以下代码: List<Employee><employee> employees = new List<Employee><employee>();中的 "<employee>"是什么意思

c# - 用于 log4net 的 CaSTLe 日志记录工具,具有流畅的 log4net 配置

xml - 在 Actionscript 3 中添加子级的循环

javascript - 如何用 HTML 解析 XML 的示例代码?

MySQL - 将数字转换为英文表示

java - 有没有更好的方法将数字转换为 Java 中具有可变十进制精度的填充字符串?

c# - 通过反射加载 C# DLL,但应用 App.config

c# - DbContext 的 ObjectContext.ApplyCurrentValues 的等价物是什么