c# - Well Formed XML using Service Stack -
i'm building mvc5 application pulls records database , allows user perform basic data cleansing edits.
once data has been cleansed needs exported xml, run through validator , uploaded third party portal.
i'm using service stack, , i've found quick , straightforward in past, particularly when outputting csv.
the 1 issue i'm having xml serialzer. i'm not sure how make generate well formed xml.
the file i'm getting dumps on 1 line, won't validate because isn't formed.
below extract controller action:
response.clear(); response.contenttype = "text/xml"; response.addheader("content-disposition", "attachment; filename="myfile.xml""); xmlserializer.serializetostream(viewmodel, response.outputstream); response.end(); update: useful comments, explained i'm not talking pretty printing, issue need run file through validator before uploading third party. error message validator throwing error:0000, xml not well-formed. cannot have more 1 tag on 1 line.
firstly, aware white space (including new lines) in xml insignificant -- has no meaning, , beautification. lack of new lines doesn't make xml ill-formed. see white space in xml documents or https://www.w3.org/tr/rec-xml/#sec-white-space. in theory shouldn't matter whether servicestack's xmlserializer putting of xml on single line.
that being said, if whatever reason must cosmetically break xml multiple lines, you'll need little work. source code can see xmlserializer uses datacontractserializer hardcoded static xmlwritersettings not allow setting xmlwritersettings.indent = true. however, since class thin wrapper on microsoft's data contract serializer, can substitute own code:
public static class datacontractserializerhelper { private static readonly xmlwritersettings xmlwritersettings = new xmlwritersettings { indent = true, indentchars = " " }; public static string serializetostring<t>(t from) { try { using (var ms = new memorystream()) using (var xw = xmlwriter.create(ms, xmlwritersettings)) { var serializer = new datacontractserializer(from.gettype()); serializer.writeobject(xw, from); xw.flush(); ms.seek(0, seekorigin.begin); var reader = new streamreader(ms); return reader.readtoend(); } } catch (exception ex) { throw new serializationexception(string.format("error serializing \"{0}\"", from), ex); } } public static void serializetowriter<t>(t value, textwriter writer) { try { using (var xw = xmlwriter.create(writer, xmlwritersettings)) { var serializer = new datacontractserializer(value.gettype()); serializer.writeobject(xw, value); } } catch (exception ex) { throw new serializationexception(string.format("error serializing \"{0}\"", value), ex); } } public static void serializetostream(object obj, stream stream) { if (obj == null) return; using (var xw = xmlwriter.create(stream, xmlwritersettings)) { var serializer = new datacontractserializer(obj.gettype()); serializer.writeobject(xw, obj); } } } and do:
datacontractserializerhelper.serializetostream(viewmodel, response.outputstream);
Comments
Post a Comment