.NET Framework Developer Center >
.NET Development Forums
>
ASMX Web Services and XML Serialization
>
XML Serialization and Line Breaks
XML Serialization and Line Breaks
- Just started working with C# and the XML classes. I've gotten as far as writing .xsd schemas, generating C# classes from those, and serializing/deserializing an XML document. I've run in to one stupid problem and can't seem to find a fix posted anywhere online. The problem is when I build a new class with data and use serialization to write it to an XML document everything is written on one line with no breaks. How can I force it to insert new lines at the appropriate places? I've tried Environment.NewLine, "\r", and "\n" for the NewLineChars property. My code is as follows:
1 public static void WriteXMLDocument(string XMLDocument, object XMLClass, Type XMLClassType) 2 { 3 XmlWriter x_write; 4 XmlSerializer x_serial = new XmlSerializer(XMLClassType); 5 XmlWriterSettings x_settings = new XmlWriterSettings(); 6 7 x_settings.NewLineChars = Environment.NewLine; 8 x_settings.NewLineOnAttributes = true; 9 x_settings.NewLineHandling = NewLineHandling.Replace; 10 x_settings.CloseOutput = true; 11 12 x_write = XmlWriter.Create(XMLDocument, x_settings); 13 x_serial.Serialize(x_write, XMLClass); 14 x_write.Close(); 15 }
- Edited byLinuxH8ter Monday, February 16, 2009 3:49 AM
Answers
- XML serialization is about persisting objects. Producing attractive, human-readable output is not something it tries to do.
On the other hand, there are many ways of producing attractive, human-readable XML (though one may question whether XML is ever truly human-readable). The simplest way I can think of is to open the XML in Internet Explorer. The second simplest would be to open it in an editor that has the ability to format XML. The XML editor in Visual Studio 2008 is very good at this, and provides for expanding and collapsing of regions of nested XML. The Express edition is even free.
John Saunders | Use File->New Project to create Web Service Projects- Marked As Answer byLucian BaciuModeratorFriday, February 20, 2009 8:45 PM
All Replies
- Why do you want to have newlines in the middle? They don't change the semantics of the XML.
If this is about pretty-printing, then I'd recommend doing that independantly of the creation of the XML.
John Saunders | Use File->New Project to create Web Service Projects - Yes, it is about pretty printing and readability. I thought setting the NewLineChars and NewLineHandling would work, but it does not. Is there any way to solve this problem while continuing to use XML serialization, and without manually writing out the XML myself?
- XML serialization is about persisting objects. Producing attractive, human-readable output is not something it tries to do.
On the other hand, there are many ways of producing attractive, human-readable XML (though one may question whether XML is ever truly human-readable). The simplest way I can think of is to open the XML in Internet Explorer. The second simplest would be to open it in an editor that has the ability to format XML. The XML editor in Visual Studio 2008 is very good at this, and provides for expanding and collapsing of regions of nested XML. The Express edition is even free.
John Saunders | Use File->New Project to create Web Service Projects- Marked As Answer byLucian BaciuModeratorFriday, February 20, 2009 8:45 PM
you were close
settings.Indent = true
settings.NewLineOnAttributes = true
http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx
- Proposed As Answer byDiggy Friday, November 06, 2009 9:01 PM
- We could ask whether programming languages are truly readable. The first time I saw C++, coming from a purely BASIC background, it looked like an alien language. What about a human language you don't know? The phase, "It's greek to me," has a real meaning. And there is a reason we have obfuscated code contest, to raise awareness that obfuscation is bad.
Programmers have to work with XML, whether to verify their output before putting it into production, or to help debug a problem. Sometimes, you have to deal with XML that has no provided schema for one reason or another (an api coding example , legacy system, fired developers, soon-to-be fired developers that don't comment...).
There is a reason we don't code in machine code. Because it ISN'T easily readable.
There is a reason we don't code in assembly when we don't have to. Because it ISN'T easily understandable.
XML is designed to be MORE readable than other formats. Otherwise, why not just have a purely binary format? Or just use base64 encoding? Unless you are really feeling a performance hit, you should try to make your XML as readable as possible. If it hits a web service and comes back as an exception, someone is going to have to read it and try to decode it. And if VS can't figure out your XML schema, it will try to put as much on one line as possible. I've had 100 mb XML files like this. All on one line. It gets a little slow. VS can handle lots of lines but not long lines. Try to debug and figure the schema of that one out without a line breaking program.


