18
Views
1
Comments
Solved
XML.XmlDocument_Save does unwanted indent, causing parsing error
Question

The problem involves the XML extension of the system components:


I have a nice XML library application built so we can easily edit XML with the use of XPath.

However, when i try to change an element it returns the xml with unwanted indent, causing errors in the external API i send the XML to.

This is because it returns an empty element like this:

......<element>[crlf]         

......</element> [crlf]

Ofcourse the . stands for a space and the [crlf] stands for a return

The API tries to set these spaces in the element, but that element is a double, so we get a parsing error.


I analysed the XML extention, function XmlDocument_Save and it says this:

        public void MssXmlDocument_Save(object ssXmlDocument, out string ssXml) {

            XmlDocument doc = ssXmlDocument as XmlDocument;

            using (MemoryStream stream = new MemoryStream()) {

                using (XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8)) {

                    writer.Formatting = System.Xml.Formatting.Indented;

                    doc.Save(writer);

                    stream.Position = 0;

                    using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8)) {

                        ssXml = reader.ReadToEnd();

                    }

                }

            }

        }

 Is there a way to work around this or can we report this as a bug or wanted feature (to be able to tell the function whether we want indent or not) ?

           

Edit:

I will add 2 files later on: one with the xml before and one with the xml after the change.


2020-09-01 10-42-42
Stefano Valente
Solution

I added the xml before and after.

My solution will be to create an extension with only the documentsave where there will be no indent.

XMLAfterSave.xml
XML_BeforeSave.xml
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.