I save the xml at the specified path and name, which I thought in pathXmlFileWrite, overwrites the file with the same name, but displays it not as an xml file.

XmlSerializer formatter = new XmlSerializer(typeof(Report)); //получаем поток, куда будем записывать сериализованный объект using (FileStream fs = new FileStream(pathXmlFileWrite, FileMode.OpenOrCreate)) { formatter.Serialize(fs, report); } 

enter image description here

enter image description here

enter image description here

enter image description here

  • By the code, everything seems to be correct, the only thing that you didn’t close, but I doubt that this is the case - Prvz
  • 2
    Please stop heading to push the whole question. The title should be short, and the text of the question should reveal the whole point. And do not duplicate the text of the question in the title. - ixSci
  • 2
    @ SVD102 so XML seems to be correct, maybe it's in IE? - Prvz
  • @Prvz What is IE? - SVD102
  • @ SVD102 Internet Explorer, with which you open your XML, and in which it is displayed incorrectly. - Nick Volynkin

2 answers 2

You are trying to open an XML file in a browser. Because XML and HTML are pretty close in structure, the browser tries to render your XML as if it were an HTML document.

Therefore, all XML tags are recognized as invalid HTML tags and are safely cut out along with all their attributes. All that remains is the contents of the <tagname> value </tagname> . These are numbers, you see them on the IE page.

Do not open XML in the browser. And in general, nothing but web pages in it do not open. To view the code and data in various formats, it is convenient to use tools specially designed for development. On Windows, for example, notepad ++, sublime text, any integrated development environment (Visual Studio)

  • ♦ How to fix it? - SVD102
  • The document is on the right, I can not open it with my program. - SVD102
  • ♦ Found a bug like this. Not correctly preserves the encoding when reading, and then it is used when writing. For some reason, capital letters become normal and a dash is removed. Before reading <? Xml version = "1.0" encoding = "ISO8859-1"?> And after writing <? Xml version = "1.0" encoding = "iso-8859-1 "?> - SVD102

Internet Explorer (IE) can be used to view XML, it uses Microsoft's native parser for this and builds a full XML tree with the ability to expand and collapse nodes, which is quite convenient when you need to quickly see the result without editing. This does not concern other browsers, although perhaps one of them is also able to. Chome - can not exactly.

At the same time, IE is quite capricious about the coding and validity of markup . For example, a bare, but valid XML without a header indicating the version and encoding IE can show, but only if the file is in UTF8, the header with this encoding will be added by IE itself. With a different encoding and without a header, or simply invalid XML, IE will not show, will more accurately show a blank page.

In your case, the coding is not specified in the header and it is very similar that IE simply does not recognize or recognize incorrectly separate characters using the default encoding, in the absence of direct instructions and therefore shows how it can, discarding all that is incomprehensible. In addition, it is extremely important that the encoding specified in the header and the actual file encoding match.

It is worth paying attention, XML can contain links to XSLT , which will automatically pull up and use the parser, and consequently, IE will show the converted document. This is perhaps the only reason why IE should be used in relation to XML, although there are special tools for this, but I haven’t yet seen any convenient and free tools.

Another option is XML with an indication of exactly what to open it , for example, MS Office documents saved as XML. It is exotic though, but it is better to be ready for such surprises.

Well, perhaps you should not use IE to view large XML , because it will be very slow and sad, and it will devour a lot of RAM.