2010-11-27 10 views
9

यह एक कॉस्मेटिक बदलाव है जो मैं बनाना चाहता था और मैं सोच रहा था कि मैं यूटीएफ -8 लोअरकेस के बजाय यूटीएफ -8 अपरकेस के साथ जेनरेट की गई XML फ़ाइल कैसे बना सकता हूं?अपरकेस में 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(); 

उत्तर

18

मुझे 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; 
} 

आप विधि XDocument सहेजें करने के लिए गंतव्य के रूप में एक XMLTextWriter उपयोग करने की आवश्यकता इस कस्टम एन्कोडिंग का उपयोग करें।

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

यह अच्छी तरह से धन्यवाद। – Prix

+1

धन्यवाद! मैंने आपके कोड को एक एक्सएमएल कक्षा में लिखा था जिसे मैंने लिखा था। यह अन्य सामान भी करता है, जैसे कि यूटीएफ -8 या यूटीएफ -16 में आउटपुट करने की अनुमति दें और एक्सएमएल को सुंदर या रैखिक बनाएं। http://www.rhyous.com/2015/04/07/an-xml-class-to-linearize-xml-make-pretty-xml-and-encoding-in-utf-8-or-utf-16 / – Rhyous

संबंधित मुद्दे