1

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?

eitanx
  • 13
  • 1
  • 3

3 Answers3

3

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    I know that both forms are valid xml, but unfortunately I'm using the diff program of tortoise_svn, since all my xml files are in a svn repository and that's why I need this kind of (unnecessary for xml) precession. – eitanx Oct 11 '11 at 15:08
  • 1
    So, it makes sense to you to change your source code because of your repository? – John Saunders Oct 11 '11 at 15:22
  • No, of course not. I thought that there will be some kind of setting that I can change that will remove the white-space, but I guess that the only solution is to check in (commit) the file with the extra white-space to the repository, that way I have the same form of empty elements in the repository. – eitanx Oct 11 '11 at 15:30
  • That, and make a feature request that they permit per-file-type settings to the diff utility. They might even want to permit you to choose the utility to use for each file type, if not on a file-by-file basis. – John Saunders Oct 11 '11 at 15:31
  • 2
    I would use Beyond Compare. It has the ability to do contextual logic-based comparisons, and will solve this and many other comparison problems. A developer license is around $30. – cwharris Oct 11 '11 at 17:52
  • 1
    Beyond compare is a powerful comparison tool and my company has a license for it. – eitanx Oct 12 '11 at 07:18
2

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(" />", "/>");
cwharris
  • 17,835
  • 4
  • 44
  • 64
  • I tried your way but there is no change: `w.WriteStartElement("description"); w.WriteEndElement(); w.ToString().Replace(" />", "/>");` – eitanx Oct 11 '11 at 15:17
  • @eitanx, the Replace method returns the modified XML string, it does not change the XML in-place. – cwharris Oct 11 '11 at 15:21
  • so I need to use something like writeraw(), in order to change the xml file and that something I can't do, since writeraw() disrupts all the new line charecters in all the siblings of . – eitanx Oct 11 '11 at 15:26
  • You would need to use the "myModifiedXmlString" and compare that instead of using the "xml". – cwharris Oct 11 '11 at 17:54
2

Look at XmlWriter.WriteRaw, e.g.:

w.WriteRaw("<description/>");
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • I tried using writeraw, but it disrupts all the new line charecters in all the siblings of and I get a single line with all the sibling elements of in the father element of : <> – eitanx Oct 11 '11 at 15:14