Sunday, March 20, 2011

XmlWriter.WriteRaw indentation

Hey

I have a similar question like http://stackoverflow.com/questions/858630/xml-indenting-when-injecting-an-xml-string-into-an-xmlwriter.

I also use XmlWriter.WriteRaw to output an XML fragment into an xml node. This document fragment does not follow the idention which would be nice. The answer given on the post above does not work for XML fragments. Well, I can read the fragment with the XmlReader, but WriteNode does not work with document fragments (see http://msdn.microsoft.com/en-us/library/1wd6aw1b.aspx).

Any hints on how to cheat to get proper indentation?

Thanks for any hints

From stackoverflow
  • You could build a valid Xml-Document in memory containing your xml fragment. Then read this document with the XmlReader (e.g. by using MemoryStream) and let the XmlWriter write indented xml.

    A faster approach wold be indenting the xml yourself by manipulating the string. Search for <, increase the nesting level and add the indention spaces. If you find </ or a self closing tag decrease the nesting level and append a \n

    I don't think there is a fast and nice solution to your problem, but i might be wrong...

    Scoregraphic : I don't think there's a nice solution at all. If it was there someone would have found it by now. But I accept your answer as you provide possibilities. Indentation does no functionality at all, so it's just for the sake of readability. I thought of the use of an xsl transformation after writing the document. See http://social.msdn.microsoft.com/Forums/en/msxml/thread/61f88cf8-c019-423b-9a88-a065a4b0566d for possible transformations.

0 comments:

Post a Comment