Home > Enterprise >  Removing empty space from closing XML tag
Removing empty space from closing XML tag

Time:01-19

Below is a code snippet of creating a document:

CdtrAcct = new CdtrAcct
{
    Id = new Id
    {
        IBAN = iban,
        Othr = new Othr
        {
            Id = creditBankAcct
        },
    },
},

If the IBAN field has a value, then Id is null. However, when the XML file is formed, I get the below:

<CdtrAcct>
  <Id>
    <IBAN>XXXXXXXXXXXXXXXXXXX</IBAN>
    <Othr />
  </Id>
</CdtrAcct>

The problem that I have is that the software that reads the XML cannot process the whitespace here: <Othr />. What do I need to do to get <Othr/>?

C# code:

XmlSerializer serializer = new XmlSerializer(typeof(Document));                               
var textWriter = new StreamWriter(@"C:\BankFiles\Outbox\"   filename   ".xml"); 
serializer.Serialize(textWriter, config);
textWriter.Close();

CodePudding user response:

Convert the XML to a string (e.g. myString), then you can replace " \>" with "\>" by using

myString.Replace(" \>", "\>");

Afterwards you can print it into a file.

Of course this is a workaround / hack and getting the buggy software fixed should be the first try. However, this should solve your problem immediately.

CodePudding user response:

It's best to avoid processing XML with anything other than a conformant XML parser.

But if you're stuck with it, it's easy enough to put the XML through an identity transformation that re-serializes it without the whitespace.

  • Related