Tab characters in an XML file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JB

    Tab characters in an XML file

    Hi everyone,

    I'm having problems with Tab characters in an XML file.
    I want to store a Tab character as part of an element value e.g.
    <Separator>TA B</Separator>
    I'm using XmlWriter to write my file and XmlReader to read it.

    At first my tab character was stored in the element, but was ignored
    when it was read back (giving me an empty string as a content).
    Herfried K. Wagner suggested on this group to use the
    'xml:space="pre serve"' attribute. It worked fine, except that now I
    have to generate my whole file without indentation otherwise the
    reader generates an exception.

    Ideally I would like
    1) To be able to store Tab characters in an element value
    2) Have an indented XML file e.g.

    <Config>
    <Some Container Element>
    <Separator>TA B</Separator>
    </Some Container Element>
    <Some Other Element>
    </Some Other Element>
    <Config>

    I've tried to play with the values of the XmlReader and XmlWriter
    settings, but can only manage to get either 1) or 2) but not both at
    the same time.

    Can somebody help me with that?

    Thanks
    JB
  • Cor Ligthert[MVP]

    #2
    Re: Tab characters in an XML file

    JB,

    Probably you need an &arg litteral. I don't know which it is, but as I have
    to do it as well can you search for it on Internet yourself.

    Cor

    "JB" <jb.brossard@gm ail.comschreef in bericht
    news:369a0fc3-8c25-4726-9b03-36101b0f3d1e@d7 7g2000hsb.googl egroups.com...
    Hi everyone,
    >
    I'm having problems with Tab characters in an XML file.
    I want to store a Tab character as part of an element value e.g.
    <Separator>TA B</Separator>
    I'm using XmlWriter to write my file and XmlReader to read it.
    >
    At first my tab character was stored in the element, but was ignored
    when it was read back (giving me an empty string as a content).
    Herfried K. Wagner suggested on this group to use the
    'xml:space="pre serve"' attribute. It worked fine, except that now I
    have to generate my whole file without indentation otherwise the
    reader generates an exception.
    >
    Ideally I would like
    1) To be able to store Tab characters in an element value
    2) Have an indented XML file e.g.
    >
    <Config>
    <Some Container Element>
    <Separator>TA B</Separator>
    </Some Container Element>
    <Some Other Element>
    </Some Other Element>
    <Config>
    >
    I've tried to play with the values of the XmlReader and XmlWriter
    settings, but can only manage to get either 1) or 2) but not both at
    the same time.
    >
    Can somebody help me with that?
    >
    Thanks
    JB

    Comment

    • Cor Ligthert[MVP]

      #3
      Re: Tab characters in an XML file

      Duh,

      did not read the message from Stephany yet, it is of course &amp

      Cor

      "Cor Ligthert[MVP]" <notmyfirstname @planet.nlschre ef in bericht
      news:A74B01BF-206D-41B0-92EA-4E97704DCC60@mi crosoft.com...
      JB,
      >
      Probably you need an &arg litteral. I don't know which it is, but as I
      have to do it as well can you search for it on Internet yourself.
      >
      Cor
      >
      "JB" <jb.brossard@gm ail.comschreef in bericht
      news:369a0fc3-8c25-4726-9b03-36101b0f3d1e@d7 7g2000hsb.googl egroups.com...
      >Hi everyone,
      >>
      >I'm having problems with Tab characters in an XML file.
      >I want to store a Tab character as part of an element value e.g.
      ><Separator>TAB </Separator>
      >I'm using XmlWriter to write my file and XmlReader to read it.
      >>
      >At first my tab character was stored in the element, but was ignored
      >when it was read back (giving me an empty string as a content).
      >Herfried K. Wagner suggested on this group to use the
      >'xml:space="pr eserve"' attribute. It worked fine, except that now I
      >have to generate my whole file without indentation otherwise the
      >reader generates an exception.
      >>
      >Ideally I would like
      >1) To be able to store Tab characters in an element value
      >2) Have an indented XML file e.g.
      >>
      ><Config>
      ><Some Container Element>
      ><Separator>TAB </Separator>
      ></Some Container Element>
      ><Some Other Element>
      ></Some Other Element>
      ><Config>
      >>
      >I've tried to play with the values of the XmlReader and XmlWriter
      >settings, but can only manage to get either 1) or 2) but not both at
      >the same time.
      >>
      >Can somebody help me with that?
      >>
      >Thanks
      >JB
      >

      Comment

      • Stephany Young

        #4
        Re: Tab characters in an XML file

        No Cor, it is NOT &amp. It IS &amp;.

        The semi-colon is not for decoration, it is critical.

        Any, if you're talking about a TAB character, there is no way it is &amp;.
        It would be &#x9;.

        & = escape character
        # = numeric literal
        x = hexadecimal
        9 = ASCII (also ANSI) for TAB character
        ; = escape sequence terminator


        "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
        news:5CE682CE-D4DC-4D8A-B1AA-835C3533C983@mi crosoft.com...
        Duh,
        >
        did not read the message from Stephany yet, it is of course &amp
        >
        Cor
        >
        "Cor Ligthert[MVP]" <notmyfirstname @planet.nlschre ef in bericht
        news:A74B01BF-206D-41B0-92EA-4E97704DCC60@mi crosoft.com...
        >JB,
        >>
        >Probably you need an &arg litteral. I don't know which it is, but as I
        >have to do it as well can you search for it on Internet yourself.
        >>
        >Cor
        >>
        >"JB" <jb.brossard@gm ail.comschreef in bericht
        >news:369a0fc 3-8c25-4726-9b03-36101b0f3d1e@d7 7g2000hsb.googl egroups.com...
        >>Hi everyone,
        >>>
        >>I'm having problems with Tab characters in an XML file.
        >>I want to store a Tab character as part of an element value e.g.
        >><Separator>TA B</Separator>
        >>I'm using XmlWriter to write my file and XmlReader to read it.
        >>>
        >>At first my tab character was stored in the element, but was ignored
        >>when it was read back (giving me an empty string as a content).
        >>Herfried K. Wagner suggested on this group to use the
        >>'xml:space="p reserve"' attribute. It worked fine, except that now I
        >>have to generate my whole file without indentation otherwise the
        >>reader generates an exception.
        >>>
        >>Ideally I would like
        >>1) To be able to store Tab characters in an element value
        >>2) Have an indented XML file e.g.
        >>>
        >><Config>
        >><Some Container Element>
        >><Separator>TA B</Separator>
        >></Some Container Element>
        >><Some Other Element>
        >></Some Other Element>
        >><Config>
        >>>
        >>I've tried to play with the values of the XmlReader and XmlWriter
        >>settings, but can only manage to get either 1) or 2) but not both at
        >>the same time.
        >>>
        >>Can somebody help me with that?
        >>>
        >>Thanks
        >>JB
        >>
        >

        Comment

        • Martin Honnen

          #5
          Re: Tab characters in an XML file

          JB wrote:
          I'm having problems with Tab characters in an XML file.
          I want to store a Tab character as part of an element value e.g.
          <Separator>TA B</Separator>
          I'm using XmlWriter to write my file and XmlReader to read it.
          >
          At first my tab character was stored in the element, but was ignored
          when it was read back (giving me an empty string as a content).
          Can you show us your code? I can't reproduce the problem, the following
          test code

          Dim fileName As String = "..\..\XmlTest1 .xml"

          Dim writerSettings As New XmlWriterSettin gs()
          writerSettings. Indent = True

          Using writer As XmlWriter = XmlWriter.Creat e(fileName,
          writerSettings)
          writer.WriteSta rtDocument()
          writer.WriteSta rtElement("root ")
          writer.WriteSta rtElement("foo" )
          writer.WriteEle mentString("bar ", ChrW(9).ToStrin g())
          writer.WriteEnd Document()
          End Using

          Using reader As XmlReader = XmlReader.Creat e(fileName)
          While reader.Read()
          If reader.NodeType = XmlNodeType.Ele ment And
          reader.LocalNam e = "bar" Then
          Dim bar As String = reader.ReadStri ng()
          Console.WriteLi ne("|{0}|", bar)
          Console.WriteLi ne(bar = ChrW(9).ToStrin g())
          End If
          End While
          End Using

          results in the output

          | |
          True

          suggesting that the tab character is written and read back.

          Make sure you don't use an XmlReader with XmlReaderSettin gs where
          IgnoreWhitespac e is set to True as in that case the tab would be ignored.


          --

          Martin Honnen --- MVP XML

          Comment

          • JB

            #6
            Re: Tab characters in an XML file

            On 15 Jun, 15:09, Martin Honnen <mahotr...@yaho o.dewrote:
            JB wrote:
            I'm having problems with Tab characters in an XML file.
            I want to store a Tab character as part of an element value e.g.
            <Separator>TA B</Separator>
            I'm using XmlWriter to write my file and XmlReader to read it.
            >
            At first my tab character was stored in the element, but was ignored
            when it was read back (giving me an empty string as a content).
            >
            Can you show us your code? I can't reproduce the problem, the following
            test code
            >
            Dim fileName As String = "..\..\XmlTest1 .xml"
            >
            Dim writerSettings As New XmlWriterSettin gs()
            writerSettings. Indent = True
            >
            Using writer As XmlWriter = XmlWriter.Creat e(fileName,
            writerSettings)
            writer.WriteSta rtDocument()
            writer.WriteSta rtElement("root ")
            writer.WriteSta rtElement("foo" )
            writer.WriteEle mentString("bar ", ChrW(9).ToStrin g())
            writer.WriteEnd Document()
            End Using
            >
            Using reader As XmlReader = XmlReader.Creat e(fileName)
            While reader.Read()
            If reader.NodeType = XmlNodeType.Ele ment And
            reader.LocalNam e = "bar" Then
            Dim bar As String = reader.ReadStri ng()
            Console.WriteLi ne("|{0}|", bar)
            Console.WriteLi ne(bar = ChrW(9).ToStrin g())
            End If
            End While
            End Using
            >
            results in the output
            >
            | |
            True
            >
            suggesting that the tab character is written and read back.
            >
            Make sure you don't use an XmlReader with XmlReaderSettin gs where
            IgnoreWhitespac e is set to True as in that case the tab would be ignored.
            >
            --
            >
            Martin Honnen --- MVP XML
            http://JavaScript.FAQTs.com/
            Hi All,

            Thanks for all your suggestions.
            Following Martin's code I isolated the problem.
            I read my file sequentially and I use the function
            ReadElementCont entAsString.
            If I write the file without indentation, ReadElementCont entAsString
            works fine.
            If I write the file with indentation, ReadElementCont entAsString
            causes an exception: "'Whitespac e' is an invalid XmlNodeType. Line 3,
            position 8."

            I don't really want to have to rewrite all my code to look like Method
            2, so I'm thinking that it might be simpler to escape all my string
            contents to transform any special character into the escaped version.
            Is there an option to do this automatically for my entire XML file or
            do I have to do it "manually" for each field with some sort of
            replace?

            See my code below:

            Using reader As XmlReader = XmlReader.Creat e(fileName)
            reader.ReadStar tElement("root" )
            reader.ReadStar tElement("foo")

            'Method 1 - Doesn't work
            ' Dim bar As String = reader.ReadElem entContentAsStr ing("bar",
            "")

            'Method 2 - Works!
            reader.Read()
            Debug.Assert(re ader.NodeType = XmlNodeType.Ele ment And
            reader.LocalNam e = "bar")
            Dim bar As String = reader.ReadStri ng()

            Console.WriteLi ne("|{0}|", bar)
            Console.WriteLi ne(bar = ChrW(9).ToStrin g())

            reader.ReadEndE lement()
            reader.ReadEndE lement()
            End Using

            Comment

            • Martin Honnen

              #7
              Re: Tab characters in an XML file

              JB wrote:
              Following Martin's code I isolated the problem.
              I read my file sequentially and I use the function
              ReadElementCont entAsString.
              If I write the file without indentation, ReadElementCont entAsString
              works fine.
              If I write the file with indentation, ReadElementCont entAsString
              causes an exception: "'Whitespac e' is an invalid XmlNodeType. Line 3,
              position 8."
              Well that exception has nothing to do with the 'bar' element containing
              a tab character or not. It occurs because
              reader.ReadStar tElement("foo")
              after this call the reader is positioned on a node of type Whitespace
              and therefore ReadElementCont entAsString() is not an allowed operation.
              For that to work you first have to make sure the reader is positioned on
              the start element.


              --

              Martin Honnen --- MVP XML

              Comment

              • Martin Honnen

                #8
                Re: Tab characters in an XML file

                JB wrote:
                Thanks for all your suggestions.
                Following Martin's code I isolated the problem.
                I read my file sequentially and I use the function
                ReadElementCont entAsString.
                If I write the file without indentation, ReadElementCont entAsString
                works fine.
                If I write the file with indentation, ReadElementCont entAsString
                causes an exception: "'Whitespac e' is an invalid XmlNodeType. Line 3,
                position 8."
                If you want to use the XmlReader in a way symmetric to the XmlWriter use
                I suggested then you can use

                reader.ReadStar tElement("root" )
                reader.ReadStar tElement("foo")
                Dim bar As String = reader.ReadElem entString("bar" )





                --

                Martin Honnen --- MVP XML

                Comment

                Working...