Jump to content

formatting with xmlwriter


perry59

Recommended Posts

I am using a little function (below) to format some xml before writing it out to file. It works pretty good, but there is one thing I'd like to change if possible. When it encounters a node without a value, it breaks it out to two lines. I want it to stay on one line. For example, it will format a node as such:<emptytag></emptytag>I want it to be as such:<emptytag></emptytag>Is it possible to get xmlwriter to do this?

static public void Beautify(XmlDocument doc) { try{StringBuilder sb = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = ""; //this seems to do nothing//settings.IndentChars = (ControlChars.Tab)settings.NewLineChars = "\r\n"; settings.NewLineHandling = NewLineHandling.Replace;XmlWriter writer = XmlWriter.Create(sb, settings);doc.Save(writer);writer.Flush();writer.Close();}catch{MessageBox.Show("Could not beautify " + doc.BaseURI);}}

Link to comment
Share on other sites

Are you sure the XmlDocument doesn't already contain that new line? If it does, XmlWriter can't ignore it.You can however use the XmlReader setting to IngoreWhiteSpace upon reading, and then Indent at write. The result should be the loss of the kind of spaces you want.

Link to comment
Share on other sites

Are you sure the XmlDocument doesn't already contain that new line? If it does, XmlWriter can't ignore it.You can however use the XmlReader setting to IngoreWhiteSpace upon reading, and then Indent at write. The result should be the loss of the kind of spaces you want.
Yes I'm sure my input file does not contain the whitespace.I may try the xmlread method and see how that doesThanks
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...