How make XMLDocument do not put spaces on self-closed tags?
Asked Answered
A

4

7

I have an XML well formatted without any spaces. It' must be like that.

When I load it to XMLDocument to sign, the self-closing tags gets an extra white space and

    <cEAN/>

becomes:

     <cEAN />

Once this document must be signed, it's impossible to remove the white space.

The property PreserveWhiteSpace doesn't made any difference to the result.

How can I change this behavior?

Aberrant answered 3/2, 2011 at 0:32 Comment(6)
I'm curious. What is consuming the XML such that it can't deal with the spaces?Crumpet
It was my understanding that the space before the forwardslash was dictated by the standard.Protege
@Bradley: no. I don't believe the standard dictates serialization format to that degree.Bianco
What sort of signature is required? XML Digital Signature?Bianco
Digital Signature with an X509 CertificateParavane
This one?Bianco
B
1

There is no space before the closing "/" in the XmlDocument. XmlDocument is a data structure consisting of nodes. It is binary. It is not text.

Any extra space you are seeing exists only when you serialize the document as text.

Are you actually having a problem with signing, or do you only think you will have such a problem?

Bianco answered 3/2, 2011 at 0:34 Comment(3)
You are right about the serialization. And I'm having a problem with an Governmental Agency, who demmands the extra space to be removed.Paravane
Tell them to go learn XML. Remind them it's an International standard.Bianco
@spender: I know it's hard (ok, impossible), but we've got to try, otherwise, we'll be back to having no standards at all. I remember those days. The market for Software Engineers was much smaller back then, and lack of standardization was one of the main reasons.Bianco
C
0

I have had this problem before. XML signed by a basic Hash so it can't change when serialized. I solved it by writing a serializer so that I could be sure that it would output the correct XML.

The basic recipe is to Read the XML with a XMLReader, and write out each chunk as it comes.

Congressional answered 3/2, 2011 at 0:45 Comment(0)
R
0

Try this:

XMLDocument doc;

...

string XMLstring = doc.OuterXml.Replace(" />","/>");
Rios answered 13/5, 2013 at 13:28 Comment(0)
M
0

The way I figured it out is as follows:

  1. Create a stream:

    var stream = new MemoryStream();

  1. Create your XML and save it to the stream:

           var writerSettings = new XmlWriterSettings
            {
                Indent = true,
                IndentChars = "    ",
                NewLineChars = Environment.NewLine,
                NewLineHandling = NewLineHandling.Replace,
                OmitXmlDeclaration = true,
            };

            using (var xrmService = new XrmServiceContext(context.Service))
            using (XmlWriter writer = XmlWriter.Create(stream, writerSettings))
            {
                writer.WriteStartElement("xyz");
                writer.WriteElementString("abc", "123");
                ...
                writer.WriteEndElement();
                writer.Flush();
            }

  1. Convert Stream to String in order to be able reformat

    string stringXml = Encoding.UTF8.GetString(stream.ToArray()).Replace(" />", "/>");

  1. Convert String back to Stream in order to be able to write to file

    
     byte[] byteArray = Encoding.UTF8.GetBytes(stringXml);
           MemoryStream formattedStream = new MemoryStream(byteArray);
    
  2. Write Stream to file


    using (FileStream file = new FileStream(@"filepath\file.xml", 
               FileMode.Create, FileAccess.Write))
                {
                    formattedStream.CopyTo(file);
                    file.Flush();
                }
                formattedStream.Flush();

Resources that has help me to achieve this:

Meeker answered 19/6, 2023 at 2:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.