c# - Remove Insignificant Whitespace From Xml Document -
i've got xml file contains both significant , insignificant whitespace. read file in , write file out disk, removing insignificant whitespace. when considering significant vs insignificant whitespace, want make sure spaces within mixed content elements (as defined schema) maintained. how can using c#?
based on this article read, sounded might need use validating xml reader reader know elements contain mixed content per schema. below 1 example of code tried did not maintain significant whitespace between elements within parent element containing mixed content.
xmldocument doc = new xmldocument(); doc.preservewhitespace = true; using (xmltextreader rdr = new xmltextreader(xmlfilepath1)) { rdr.whitespacehandling = whitespacehandling.significant; xmlschemaset sc = new xmlschemaset(); sc.add(null, @"c:\my-schema.xsd"); doc.load(xmlreader.create(rdr, new xmlreadersettings() { validationtype = system.xml.validationtype.schema, schemas = sc })); using (xmltextwriter writer = new xmltextwriter(xmlfilepath2, encoding.utf8)) { writer.formatting = formatting.none; doc.save(writer); } }
below example of xml maintain whitespace. expecting whitespace between 2 inline tag elements maintained. para defined in schema having mixed content.
<para>i have bunch of text here <inlinetag attrib="a"/> <inlinetag attrib="b"/></para>
here simple example of whitespace want remove. in example, markup came printy printed xml document tabs or spaces in front of elements , line breaks after them. want keep whitespace inside para not outside of it.
<entry colname="col2"> <para>lorem ipsum dolor sit amet, et nulla diam.<inlinetag value="a"/> <inlinetag value="b"/></para> </entry>
i did find solution writing significant whitespace, although straight-forward had hoped. below example code borrows heavily mark fussell's blog. modification made writeshallownode method remove case statement xmlnodetype.whitespace. causes insignificant whitespace removed output.
using (var xwmodifiedxml = xmlwriter.create(xmlfilepath1)) { using (var sr = new stringreader(file.readalltext(xmlfilepath2))) { xmlschemaset sc = new xmlschemaset(); sc.add(null, @"c:\my-schema.xsd"); using (var xrmodifiedxml = xmlreader.create(sr, new xmlreadersettings() { validationtype = system.xml.validationtype.schema, schemas = sc })) { bool readresult = xrmodifiedxml.read(); while (readresult) { writeshallownode(xrmodifiedxml, xwmodifiedxml); readresult = xrmodifiedxml.read(); } } } } static void writeshallownode( xmlreader reader, xmlwriter writer ) { if ( reader == null ) { throw new argumentnullexception("reader"); } if ( writer == null ) { throw new argumentnullexception("writer"); } switch ( reader.nodetype ) { case xmlnodetype.element: writer.writestartelement( reader.prefix, reader.localname, reader.namespaceuri ); writer.writeattributes( reader, true ); if ( reader.isemptyelement ) { writer.writeendelement(); } break; case xmlnodetype.text: writer.writestring( reader.value ); break; case xmlnodetype.significantwhitespace: // write significant whitespace writer.writewhitespace(reader.value); break; case xmlnodetype.cdata: writer.writecdata( reader.value ); break; case xmlnodetype.entityreference: writer.writeentityref(reader.name); break; case xmlnodetype.xmldeclaration: case xmlnodetype.processinginstruction: writer.writeprocessinginstruction( reader.name, reader.value ); break; case xmlnodetype.documenttype: writer.writedoctype( reader.name, reader.getattribute( "public" ), reader.getattribute( "system" ), reader.value ); break; case xmlnodetype.comment: writer.writecomment( reader.value ); break; case xmlnodetype.endelement: writer.writefullendelement(); break; } }
Comments
Post a Comment