Does anyone know how to do this easily?
The only thing I can think of is to loop through each element / attribute & use the XmlWriter class. But that's far from easy.
-
-
Minh wrote:Does anyone know how to do this easily?
The only thing I can think of is to loop through each element / attribute & use the XmlWriter class. But that's far from easy.
What is beautify? -
You mean, canonically indent?
-
H4L0PR1CK wrote:

Minh wrote:Does anyone know how to do this easily?
The only thing I can think of is to loop through each element / attribute & use the XmlWriter class. But that's far from easy.
What is beautify?
I think he means to give the XML document lots of nice tabs and indentation.
The thing is it's hard unless you know which elements are inline and which are block (and which are more esoteric types like inline-block and table-cell) which give you a good idea how the elements are tabbified in the XML source.
VS2003/VS2005 does it nicely, there's a "format document" button that formats them nicely.
-
Just load the XML into an XmlDocument object then write the document using an XmlTextWriter with the indent and whitespace settings you require.
-
W3bbo wrote:

H4L0PR1CK wrote: 
Minh wrote: Does anyone know how to do this easily?
The only thing I can think of is to loop through each element / attribute & use the XmlWriter class. But that's far from easy.
What is beautify?
I think he means to give the XML document lots of nice tabs and indentation.
The thing is it's hard unless you know which elements are inline and which are block (and which are more esoteric types like inline-block and table-cell) which give you a good idea how the elements are tabbified in the XML source.
VS2003/VS2005 does it nicely, there's a "format document" button that formats them nicely.
Ahh, yes. Use VS or Stylus Studio or some other tool.
Couldn't you also load the XML into a dataset and then use ds.writeXML() to save the xml back to disc (All the xml I have seen saved from a dataset is pretty)
Question though, why do you care if it is pretty or not? -
Yes.Maurits wrote:You mean, canonically indent?
-
thepuffin wrote:Just load the XML into an XmlDocument object then write the document using an XmlTextWriter with the indent and whitespace settings you require.
Aahh.. this seems to work. Now, does anyone know how to do this without writing to a file, but returning a string instead?
private void FormatXmlString(string xmlString)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(xmlString);
System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter("test.xml", System.Text.UTF8Encoding.UTF8);
w.Formatting = System.Xml.Formatting.Indented;
doc.WriteContentTo(w);
w.Close();
}
-
H4L0PR1CK wrote:
Question though, why do you care if it is pretty or not?
I'm debugging SOAP messages, and it's a heck of a lot easier when it's all prettied up.
-
Minh wrote:

thepuffin wrote: Just load the XML into an XmlDocument object then write the document using an XmlTextWriter with the indent and whitespace settings you require.
Aahh.. this seems to work. Now, does anyone know how to do this without writing to a file, but returning a string instead?
Simple, use a StringWriter as the base for the XmlTextWriter:
private static string FormatXml(string inputXml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(inputXml);
using( StringWriter writer = new StringWriter() )
using( XmlTextWriter xmlWriter = new XmlTextWriter(writer) )
{
xmlWriter.Formatting = Formatting.Indented;
doc.WriteContentTo(xmlWriter);
xmlWriter.Flush(); // not sure if that's necessary but it won't hurt
return writer.ToString();
}
} -
Thanks, Sven. That did the trick!
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.