Question

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?

Was it helpful?

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.

OTHER TIPS

Look at XmlWriter.WriteRaw, e.g.:

w.WriteRaw("<description/>");

Both of these forms are valid XML, and whatever system your using should be able to parse both forms, lest it not be an XML parser.

I'm not at my computer right now, but I feel like this would work:

Var myModifiedXmlString = w.ToString().Replace(" />", "/>");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top