When writing an object in xml, I get the following (see screen). How to make the record that follows the report tag and not before the eeprom tag. And how to add the following to the beginning of the file
<?xml version="1.0" encoding="ISO8859-1"?>
When writing an object in xml, I get the following (see screen). How to make the record that follows the report tag and not before the eeprom tag. And how to add the following to the beginning of the file
<?xml version="1.0" encoding="ISO8859-1"?>
Try this:
XmlSerializer s = new XmlSerializer(objectToSerialize.GetType()); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("",""); s.Serialize(xmlWriter, objectToSerialize, ns);
To define serialization settings, use the XmlWriterSetting class, which has parameters
Then your code will look like this.
XmlSerializer formatter = new XmlSerializer(typeof(Report)); // объект, который сериализуем Report obj = new Report(); // опускаем все определения пространств имен XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("",""); //получаем поток, куда будем записывать сериализованный объект using (FileStream fs = new FileStream(pathXmlFileWrite, FileMode.OpenOrCreate)) { XmlWriter writer = XmlWriter.Create(fs, new XmlWriterSettings() { OmitXmlDeclaration = false, Indent = true, Encoding = Encoding.GetEncoding("ISO-8859-1") }); formatter.Serialize(writer, obj, ns); }
In the serialization settings, we explicitly specified not to delete the XmlDeclaration and set the Latin1 (ISO8859-1) encoding via the Encoding property.
Source: https://ru.stackoverflow.com/questions/498237/
All Articles