English 中文(简体)
上限为8
原标题:utf-8 in uppercase?

这是我想要作出的一种表面变化,我很想知道,我如何把产生的xml档案交给UTF-8上案,而不是“utf-8”下案?

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Encoding = Encoding.UTF8;
        settings.Indent = true;
        settings.IndentChars = "	";

        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);
}
问题回答
writeXML.WriteProcessingInstruction("xml", "version="1.0" encoding="UTF-8"");




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签