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

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -