Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Shortcoming with XmlReader (2.0)

Shortcoming with XmlReader (2.0)

Scheduled Pinned Locked Moved XML / XSL
helpxmlquestionannouncementcsharp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #1

    I lost a few hours to this problem this morning, and after I discovered the workaround I felt like ranting, but now I've cooled down a bit (and had a beer). Anyway... I have an XML file with embedded stylesheets (XSL). One of these stylesheets transforms the XML to CSV. An important part of this transform is the insertion of linefeeds, I do this with <xsl:text>&#10;</xsl:text>, and it worked fine until I started using an XmlReader to perform a validated read. This works; the resultant XmlElement's text is a linefeed.

    System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
    doc.Load ( @"\XMLtest.xml" ) ;

    This doesn't work; the resultant XmlElement's text is empty.

    System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
    System.Xml.XmlReaderSettings rs = new System.Xml.XmlReaderSettings() ;

    (set the XmlReaderSetting's properties.)

    doc.Load ( System.Xml.XmlReader.Create ( @"\XMLtest.xml" , rs ) ) ;

    So then, looking through the help for XmlReader I see: XmlReader objects created by the Create method expand all entities automatically. So I assume that the entity gets expanded, then the linefeed (whitespace) is determined to be non-essential and removed, leaving an empty value. Looking further I see: If you must expand entities on request (readers created by the Create method expand all entities), or if you do not want your text content normalized, use the XmlTextReader class. Now wait a minute! Isn't the XmlTextReader, not recommended practice? In the Microsoft .NET Framework version 2.0 release, the recommended practice is to create XmlReader instances using the System.Xml.XmlReader.Create method. XmlReader objects created by the Create method are, by default, more conformant than the XmlTextReader implementation. So the workaround I chose is:

    System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
    System.Xml.XmlReaderSettings rs = new System.Xml.XmlReaderSettings() ;

    (set the XmlReaderSetting's properties.)

    doc.Load ( System.Xml.XmlReader.Create ( new System.Xml.XmlTextReader ( @"\XMLtest.xml" ) , rs ) ) ;

    It gets the job done, but it seems odd that there isn't a property in the XmlReaderSettings to do this. I tried the IgnoreWhitespace and CheckCharacters properties but to no avail. My question is: Does .net 3.0 solve this issue? Does anyone else have cleaner workaroun

    G 1 Reply Last reply
    0
    • P PIEBALDconsult

      I lost a few hours to this problem this morning, and after I discovered the workaround I felt like ranting, but now I've cooled down a bit (and had a beer). Anyway... I have an XML file with embedded stylesheets (XSL). One of these stylesheets transforms the XML to CSV. An important part of this transform is the insertion of linefeeds, I do this with <xsl:text>&#10;</xsl:text>, and it worked fine until I started using an XmlReader to perform a validated read. This works; the resultant XmlElement's text is a linefeed.

      System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
      doc.Load ( @"\XMLtest.xml" ) ;

      This doesn't work; the resultant XmlElement's text is empty.

      System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
      System.Xml.XmlReaderSettings rs = new System.Xml.XmlReaderSettings() ;

      (set the XmlReaderSetting's properties.)

      doc.Load ( System.Xml.XmlReader.Create ( @"\XMLtest.xml" , rs ) ) ;

      So then, looking through the help for XmlReader I see: XmlReader objects created by the Create method expand all entities automatically. So I assume that the entity gets expanded, then the linefeed (whitespace) is determined to be non-essential and removed, leaving an empty value. Looking further I see: If you must expand entities on request (readers created by the Create method expand all entities), or if you do not want your text content normalized, use the XmlTextReader class. Now wait a minute! Isn't the XmlTextReader, not recommended practice? In the Microsoft .NET Framework version 2.0 release, the recommended practice is to create XmlReader instances using the System.Xml.XmlReader.Create method. XmlReader objects created by the Create method are, by default, more conformant than the XmlTextReader implementation. So the workaround I chose is:

      System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
      System.Xml.XmlReaderSettings rs = new System.Xml.XmlReaderSettings() ;

      (set the XmlReaderSetting's properties.)

      doc.Load ( System.Xml.XmlReader.Create ( new System.Xml.XmlTextReader ( @"\XMLtest.xml" ) , rs ) ) ;

      It gets the job done, but it seems odd that there isn't a property in the XmlReaderSettings to do this. I tried the IgnoreWhitespace and CheckCharacters properties but to no avail. My question is: Does .net 3.0 solve this issue? Does anyone else have cleaner workaroun

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      I think you may need to use the xml:space attribute in you XML document. It has two values, "default" and "preserve". "default" value tells the XML processor to handle space as necessary. Also, this is the default behavior of the processor. "preserve" means to maintain whitespace as is. Also, is not an entity, and > is an entity. Example: <poem xml:space="preserve"> ... all whitespace will be preserved in all child nodes (remember text is considered a node) ... <poem/> Unfortunately, I found the following: http://www.stylusstudio.com/xmldev/200307/post70060.html#[^] -- modified at 20:16 Friday 1st June, 2007

      "We make a living by what we get, we make a life by what we give." --Winston Churchill

      P D 2 Replies Last reply
      0
      • G George L Jackson

        I think you may need to use the xml:space attribute in you XML document. It has two values, "default" and "preserve". "default" value tells the XML processor to handle space as necessary. Also, this is the default behavior of the processor. "preserve" means to maintain whitespace as is. Also, is not an entity, and > is an entity. Example: <poem xml:space="preserve"> ... all whitespace will be preserved in all child nodes (remember text is considered a node) ... <poem/> Unfortunately, I found the following: http://www.stylusstudio.com/xmldev/200307/post70060.html#[^] -- modified at 20:16 Friday 1st June, 2007

        "We make a living by what we get, we make a life by what we give." --Winston Churchill

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        I'll give that a try, thanks.

        1 Reply Last reply
        0
        • G George L Jackson

          I think you may need to use the xml:space attribute in you XML document. It has two values, "default" and "preserve". "default" value tells the XML processor to handle space as necessary. Also, this is the default behavior of the processor. "preserve" means to maintain whitespace as is. Also, is not an entity, and > is an entity. Example: <poem xml:space="preserve"> ... all whitespace will be preserved in all child nodes (remember text is considered a node) ... <poem/> Unfortunately, I found the following: http://www.stylusstudio.com/xmldev/200307/post70060.html#[^] -- modified at 20:16 Friday 1st June, 2007

          "We make a living by what we get, we make a life by what we give." --Winston Churchill

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          George L. Jackson wrote:

          Also, is not an entity, and > is an entity.

          It's character reference[^]! Awesome, I didn't know there is difference between them till now.


          "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups