c# - Ignore whitespace while reading XML -
i have xml format following format
<tag> value </tag>
this comes external datasource cannot change. when using xmlreader
content has linebreaks
, whitepace
.
xmlreadersettings xmlsettings = new xmlreadersettings(); xmlsettings.schemas = new system.xml.schema.xmlschemaset(); xmlreader schemareader = xmlreader.create(xsdstream); xmlsettings.schemas.add("", schemareader); xmlsettings.validationtype = validationtype.schema; reader = xmlreader.create(xmlfilename, xmlsettings); // parse xml file. while (reader.read()) { if (reader.isstartelement()) { switch (reader.name) { case "tag": string value = reader.readelementcontentasstring(); console.writeline(value); break; } } }
how can avoid this?
not working answer
this answer doesn't seem work, i'm leaving moment avoid else suggesting it. i'll delete if posts better answer.
did try setting xmlreadersettings.ignorewhitespace
?
white space not considered significant includes spaces, tabs, , blank lines used set apart markup greater readability. example of white space in element content.
for reason doesn't affect readelementcontentasstring
or value
property of text node.
simple answer
you call trim
:
string value = reader.readelementcontentasstring().trim();
that won't remove line breaks between contentful lines, of course... if need that, use string.replace
.
(as mentioned in comment, i'd prefer using linq xml xmlreader
unless you're genuinely reading large fit in memory, that's separate matter.)
Comments
Post a Comment