Is there a standard literal constant that I can use instead of "utf-8" in C # (.Net 3.5)?

I would like to find a better way to do this:

XmlNode nodeXML = xmlDoc.AppendChild( xmlDoc.CreateXmlDeclaration( "1.0", "utf-8", String.Empty) ); 

I do not want to think about โ€œutf-8โ€ versus โ€œUTF-8โ€ versus โ€œUTF8โ€ and โ€œutf8โ€ when I type in the code. I would like to make my code less prone to typos. I'm sure some standard library declared "utf-8" as a const / readonly line. How can I find him? Also, what about "1.0"? I assume that major versions of XML have also been listed.

Thanks!

+4
source share
3 answers

Try Encoding.UTF8.WebName .

+7
source
+2
source

Easy to avoid using XmlWriter to write a document. The writer automatically encodes in utf8 by default and generates a processing instruction:

 var doc = new XmlDocument(); doc.InnerXml = "<root></root>"; using (var wrt = XmlWriter.Create(@"c:\temp\test.xml")) { doc.WriteTo(wrt); } 

Conclusion:

 <?xml version="1.0" encoding="utf-8"?><root></root> 
+2
source

Source: https://habr.com/ru/post/1309448/


All Articles