using System.IO; using System.Xml; public static void WriteXmlWithRawString(string outputFilePath, string xmlString) { // Create a new XmlDictionaryWriter with output file path and encoding XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(outputFilePath, System.Text.Encoding.UTF8); // Write the XML string without encoding writer.WriteRaw(xmlString); // End the document and flush the writer writer.Flush(); writer.Close(); } string xmlString = ""; WriteXmlWithRawString("output.xml", xmlString); value1
using System.IO; using System.Xml; public static void WriteXmlWithRawString(string outputFilePath, string xmlString) { // Create a MemoryStream to write XML to memory MemoryStream memoryStream = new MemoryStream(); // Create a new XmlDictionaryWriter with MemoryStream and encoding XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(memoryStream, System.Text.Encoding.UTF8); // Write the XML string without encoding writer.WriteRaw(xmlString); // End the document and flush the writer writer.Flush(); writer.Close(); // Save the MemoryStream to file File.WriteAllBytes(outputFilePath, memoryStream.ToArray()); } string xmlString = "The XmlDictionaryWriter class is part of the System.Runtime.Serialization.Xml package library."; WriteXmlWithRawString("output.xml", xmlString); value1