Fer
Fer

Reputation: 1992

how to make readable xml tags


I send an XML data to a client software using TCP/IP. My software is written in C and I generate the XML data manually by string concatenations. And the client software displays the XML in a textbox.
The problem is I create a xml data like:

<root><name>My name</name><surname>My surname</surname></root>

But our customer who uses the client software wants to see this xml text more clear like:

<root>
    <name>My name</name>
    <surname>My surname</surname>
</root>

I can just add new line chars (\r\n) at the end of eachtag and some \t to make it more readable, but I don't know if it breaks the xml syntax or not.
My question is, what can I do to make the xml data more readable in a textbox while xml syntax stays correct?

Upvotes: 1

Views: 103

Answers (1)

Michael Kay
Michael Kay

Reputation: 163549

Writing XML using string concatenation is generally a bad idea. It's very easy to get it wrong (e.g. failing to escape special characters), and it means that when people ask you for prettier output, you have to enhance your code rather than just setting a switch in some library.

So you should switch to using a serialization library rather than doing it by hand.

I don't work in the C space myself, but here's a suggestion from someone who does:

C tree XML serialization

Upvotes: 2

Related Questions