Search code examples
c#xmlxmlwriterwriteelementstring

writing an empty xml element in c# using xmlwriter


I'm using xmlwriter in order to edit xml files, but I need it to maintain a specific format for future comparisons with other xml files.

I'm using the following code to write empty elements:

w.WriteStartElement("description");
w.WriteEndElement();

the result in the xml file is:

<description />

which would normally will be ok, but I need It to look like that:

<description/>

without the space character after "description".

Is there any way to do it?


Solution

  • XML is not text. The rules are different. In general, you cannot use a text comparison to compare XML files. The two examples you gave are identical XML, yet, as you noted, they are different text.

    You can begin to approach this by pre-processing the files to be compared to do things like put all attributes in the same order, perhaps one attribute per line; by placing elements into a canonical order, using the same prefix for the same namespace, etc. You then have to tell your text comparison tool to ignore whitespace, etc.