Xmlserializer generates empty file space before closing tag

Asked

Viewed 190 times

0

I am using the following code to generate an xml:

public bool SerializarObjeto(object o, string pathArquivo)
{
    var xns = new XmlSerializerNamespaces();

    XmlSerializer writer = new XmlSerializer(o.GetType());

    StreamWriter file = new StreamWriter(pathArquivo);
    writer.Serialize(file, o, xns);
    file.Close();
    return true;
}

Generates the following result in a given file:

<item iis="078906590000500400348059" produzido="2016-07-15T07:34:54" />

My problem:

I need you not to leave with empty space before closing the tag:

How does it come out: " />
How should I get out: "/>

thank you.

  • The palliative solution I adopted was to create a method to read the file and remove these spaces. I believe that this way of generating xml is from Xmlserialize itself. I’m still looking for a way to solve this in a more correct way..

1 answer

1


Well, I don’t know any options in XML objects. NET to modify this behavior, I believe that you will need to make a code in this your method to do this after serialization, something like:

var xml = System.IO.File.ReadAllText(pathArquivo);
xml = xml.Replace(" />","/>");
System.IO.File.WriteAllText(pathArquivo, xml);

Remember that if you make any digital signature in XML, you have to do this replace BEFORE signing, since it will change the contents of the file

  • 1

    Thank you @Pedro, that was exactly my solution. It worked really well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.