C# utf-8是否大写?

C# utf-8是否大写?,c#,xml,formatting,C#,Xml,Formatting,这更像是我想要做的一个表面性的改变,我想知道如何用UTF-8大写而不是UTF-8小写来生成xml文件 XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; settings.Indent = true; settings.IndentChars = "\t"; XmlWriter writeX

这更像是我想要做的一个表面性的改变,我想知道如何用UTF-8大写而不是UTF-8小写来生成xml文件

        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
{
  // 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);
}
//此部分未显示在博客中
var xDoc=XDocument.Load(xmlDocNm)//这是您的xml路径值
//此处对XML文档的更改
//.Net使用小写utf-8编写XML声明。
//  
//它并不重要,但NiceForm希望delcaration是大写的。
//  
//我们正在使用带有自定义编码的XMLWriter来对UTF进行资本化
//设置各种选项以检索所需的输出
var设置=新的XmlWriterSettings{
Encoding=new UpperCaseUTF8Encoding(),//本例的键设置选项
NewLineHandling=System.Xml.NewLineHandling.Replace,
NewLineOnAttributes=true,
Indent=true//为每个元素生成新行
};
使用(var xmlWriter=XmlTextWriter.Create(xmlDocNm,设置)){
xDoc.Save(xmlWriter);
}

谢谢!我在自己编写的Xml类中使用了您的代码。它还做其他事情,比如允许以UTF-8或UTF-16输出,并使Xml变得漂亮或线性化。欢迎来到StackOverflow。请提供一些解释,为什么你认为你提出的解决方案可能有助于OP。不要只发布没有解释的代码。
writeXML.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");