Cannot replace double quotes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R2Vvcmdl?=

    Cannot replace double quotes

    Hello,

    I have some XML that is returned to my application from another vendor that
    I cannot change before it gets to me. I can only alter it after it gets to my
    application. That being said, I am having a problem loading the XML correctly
    into my app.

    Here is the code:
    =============== =============== ======
    Dim sPaymentXML as String
    sPaymentXML = Request.Form("P aymentXML").ToS tring

    'Create the XML Document
    Dim xmlDoc As XmlDocument
    xmlDoc = New XmlDocument()

    'Load the Xml file
    xmlDoc.LoadXml( sPaymentXML)
    =============== =============== ======

    Whenever I run this code, it fails on the last line and gives an error of
    something like:
    The data at the root level is invalid. Line 1, position 1.

    I believe this is because something is off in the XML. So, I am attempting
    to replace the quotes that are in the XML doc with anything, but when the
    code runs, it just ignores the Replace command, and after several different
    attempts, I cannot get the code to recognize the double quotes that need to
    be replaced.

    Here is the code I add for replacing the quotes in the XML:
    'Replace the quotes
    sPaymentXML = sPaymentXML.Rep lace("""", "blah")

    I have also tried outputting the XML directly to a Label on the screen so I
    can examine it and it is completely valid. I have even copied the xml that
    appeared in the Label, and pasted it into VisualStudio, and run the LoadXML
    code again using the newly pasted code, and that works.

    Any ideas??
  • Rory Becker

    #2
    Re: Cannot replace double quotes

    Hello george,

    I saw this blog post just now and thought of you...




    --
    Rory
    I have some XML that is returned to my application from another vendor
    that I cannot change before it gets to me. I can only alter it after
    it gets to my application. That being said, I am having a problem
    loading the XML correctly into my app.
    >
    Here is the code:
    =============== =============== ======
    Dim sPaymentXML as String
    sPaymentXML = Request.Form("P aymentXML").ToS tring
    'Create the XML Document
    Dim xmlDoc As XmlDocument
    xmlDoc = New XmlDocument()
    'Load the Xml file
    xmlDoc.LoadXml( sPaymentXML)
    =============== =============== ======
    Whenever I run this code, it fails on the last line and gives an error
    of
    something like:
    The data at the root level is invalid. Line 1, position 1.
    I believe this is because something is off in the XML. So, I am
    attempting to replace the quotes that are in the XML doc with
    anything, but when the code runs, it just ignores the Replace command,
    and after several different attempts, I cannot get the code to
    recognize the double quotes that need to be replaced.
    >
    Here is the code I add for replacing the quotes in the XML:
    'Replace the quotes
    sPaymentXML = sPaymentXML.Rep lace("""", "blah")
    I have also tried outputting the XML directly to a Label on the screen
    so I can examine it and it is completely valid. I have even copied the
    xml that appeared in the Label, and pasted it into VisualStudio, and
    run the LoadXML code again using the newly pasted code, and that
    works.

    Comment

    • =?Utf-8?B?R2Vvcmdl?=

      #3
      Re: Cannot replace double quotes

      Thanks for the response, Rory.
      I still didn't have any luck after reading that post, though.

      I went back and tried a few million different approaches and finally found a
      way that would work. The problem does seem to be extra characters that are
      coming over, but I couldn't be certain if it was the BOM or if it was
      something else.

      First, I had outputted what I was getting from Request.Form("P aymentXML") to
      a label on my page, and it looked like perfectly fine XML.

      Next, I decided to output it to a file as other sites have suggested. So, I
      created a temp file, wrote the contents of Request.Form("P aymentXML") to
      that, and examined it. What it was doing was writing all of the special
      characters that make up the xml tags in HTML character code format. For
      example, the "<" character was being written as "<", the ">" character was
      being written as ">", and the double quotes were being written as """.
      So, I did Replace on those, and then ran that data back into my application,
      and it worked.

      Here is the code I'm talking about:
      =============== =============== =====
      'Get xml from vendor
      sPaymentXML = Request.Form("P aymentXML")

      'Convert special character codes
      sPaymentXML = sPaymentXML.Rep lace("<", "<")
      sPaymentXML = sPaymentXML.Rep lace(">", ">")
      sPaymentXML = sPaymentXML.Rep lace(""", """")

      'Create temp file
      Dim sTempFileName As String = System.IO.Path. GetTempFileName ()
      Dim fstreamTemp As New System.IO.FileS tream(sTempFile Name,
      IO.FileMode.Cre ate, IO.FileAccess.R eadWrite)
      Dim fwriterTemp As New IO.StreamWriter (fstreamTemp)

      'Write the XMl to the temp file
      With fwriterTemp
      Try
      .BaseStream.See k(0, IO.SeekOrigin.E nd)
      .WriteLine(sPay mentXML)
      Finally
      .Close()
      End Try
      End With

      fstreamTemp.Clo se()

      Dim xmlDoc As XmlDocument = New XmlDocument
      xmlDoc.Load(sTe mpFileName)
      =============== =============== =====

      When xmlDoc.Load runs now, there is no error message and I can traverse the
      XMLDocument just fine. After I'm done with the XMLDocument, I delete the temp
      file. I'm still not sure why I have to create a temp file, write the replaced
      contents of sPaymentXML into it, then load that into an new XMLDocument just
      to have it work.
      You would think I could just take the replaced contents of sPaymentXML and
      use xmlDoc.LoadXML( sPaymentXML) to achieve the same result, but that doesn't
      work.

      Just thought I'd post my finding in case it helped someone else.

      "Rory Becker" wrote:
      Hello george,
      >
      I saw this blog post just now and thought of you...
      >

      >
      >
      --
      Rory
      >
      I have some XML that is returned to my application from another vendor
      that I cannot change before it gets to me. I can only alter it after
      it gets to my application. That being said, I am having a problem
      loading the XML correctly into my app.

      Here is the code:
      =============== =============== ======
      Dim sPaymentXML as String
      sPaymentXML = Request.Form("P aymentXML").ToS tring
      'Create the XML Document
      Dim xmlDoc As XmlDocument
      xmlDoc = New XmlDocument()
      'Load the Xml file
      xmlDoc.LoadXml( sPaymentXML)
      =============== =============== ======
      Whenever I run this code, it fails on the last line and gives an error
      of
      something like:
      The data at the root level is invalid. Line 1, position 1.
      I believe this is because something is off in the XML. So, I am
      attempting to replace the quotes that are in the XML doc with
      anything, but when the code runs, it just ignores the Replace command,
      and after several different attempts, I cannot get the code to
      recognize the double quotes that need to be replaced.

      Here is the code I add for replacing the quotes in the XML:
      'Replace the quotes
      sPaymentXML = sPaymentXML.Rep lace("""", "blah")
      I have also tried outputting the XML directly to a Label on the screen
      so I can examine it and it is completely valid. I have even copied the
      xml that appeared in the Label, and pasted it into VisualStudio, and
      run the LoadXML code again using the newly pasted code, and that
      works.
      >
      >
      >

      Comment

      • =?Utf-8?B?R2Vvcmdl?=

        #4
        Re: Cannot replace double quotes

        Ooops, it looks like the formatting of this post did not allow me to show the
        HTML character codes.

        When I said:
        For example, the "<" character was being written as "<", the ">" character was
        being written as ">", and the double quotes were being written as """.

        It really should say that:
        < = & lt ; (without the spaces)
        = & gt ; (without the spaces)
        " = & quot ; (without the spaces)

        "George" wrote:
        Thanks for the response, Rory.
        I still didn't have any luck after reading that post, though.
        >
        I went back and tried a few million different approaches and finally found a
        way that would work. The problem does seem to be extra characters that are
        coming over, but I couldn't be certain if it was the BOM or if it was
        something else.
        >
        First, I had outputted what I was getting from Request.Form("P aymentXML") to
        a label on my page, and it looked like perfectly fine XML.
        >
        Next, I decided to output it to a file as other sites have suggested. So, I
        created a temp file, wrote the contents of Request.Form("P aymentXML") to
        that, and examined it. What it was doing was writing all of the special
        characters that make up the xml tags in HTML character code format. For
        example, the "<" character was being written as "<", the ">" character was
        being written as ">", and the double quotes were being written as """.
        So, I did Replace on those, and then ran that data back into my application,
        and it worked.
        >
        Here is the code I'm talking about:
        =============== =============== =====
        'Get xml from vendor
        sPaymentXML = Request.Form("P aymentXML")
        >
        'Convert special character codes
        sPaymentXML = sPaymentXML.Rep lace("<", "<")
        sPaymentXML = sPaymentXML.Rep lace(">", ">")
        sPaymentXML = sPaymentXML.Rep lace(""", """")
        >
        'Create temp file
        Dim sTempFileName As String = System.IO.Path. GetTempFileName ()
        Dim fstreamTemp As New System.IO.FileS tream(sTempFile Name,
        IO.FileMode.Cre ate, IO.FileAccess.R eadWrite)
        Dim fwriterTemp As New IO.StreamWriter (fstreamTemp)
        >
        'Write the XMl to the temp file
        With fwriterTemp
        Try
        .BaseStream.See k(0, IO.SeekOrigin.E nd)
        .WriteLine(sPay mentXML)
        Finally
        .Close()
        End Try
        End With
        >
        fstreamTemp.Clo se()
        >
        Dim xmlDoc As XmlDocument = New XmlDocument
        xmlDoc.Load(sTe mpFileName)
        =============== =============== =====
        >
        When xmlDoc.Load runs now, there is no error message and I can traverse the
        XMLDocument just fine. After I'm done with the XMLDocument, I delete the temp
        file. I'm still not sure why I have to create a temp file, write the replaced
        contents of sPaymentXML into it, then load that into an new XMLDocument just
        to have it work.
        You would think I could just take the replaced contents of sPaymentXML and
        use xmlDoc.LoadXML( sPaymentXML) to achieve the same result, but that doesn't
        work.
        >
        Just thought I'd post my finding in case it helped someone else.
        >
        "Rory Becker" wrote:
        >
        Hello george,

        I saw this blog post just now and thought of you...




        --
        Rory
        I have some XML that is returned to my application from another vendor
        that I cannot change before it gets to me. I can only alter it after
        it gets to my application. That being said, I am having a problem
        loading the XML correctly into my app.
        >
        Here is the code:
        =============== =============== ======
        Dim sPaymentXML as String
        sPaymentXML = Request.Form("P aymentXML").ToS tring
        'Create the XML Document
        Dim xmlDoc As XmlDocument
        xmlDoc = New XmlDocument()
        'Load the Xml file
        xmlDoc.LoadXml( sPaymentXML)
        =============== =============== ======
        Whenever I run this code, it fails on the last line and gives an error
        of
        something like:
        The data at the root level is invalid. Line 1, position 1.
        I believe this is because something is off in the XML. So, I am
        attempting to replace the quotes that are in the XML doc with
        anything, but when the code runs, it just ignores the Replace command,
        and after several different attempts, I cannot get the code to
        recognize the double quotes that need to be replaced.
        >
        Here is the code I add for replacing the quotes in the XML:
        'Replace the quotes
        sPaymentXML = sPaymentXML.Rep lace("""", "blah")
        I have also tried outputting the XML directly to a Label on the screen
        so I can examine it and it is completely valid. I have even copied the
        xml that appeared in the Label, and pasted it into VisualStudio, and
        run the LoadXML code again using the newly pasted code, and that
        works.

        Comment

        • Rory Becker

          #5
          Re: Cannot replace double quotes

          Hello george,
          >'Get xml from vendor
          >sPaymentXML = Request.Form("P aymentXML")
          Try...
          -------------------------------------------------------------
          sPaymentXML = System.Web.Http ServerUtility.H tmlDecode(Reque st.Form("Paymen tXML"))
          -------------------------------------------------------------

          I think the vendor or some intermediate process might have HtmlEncoded your
          XML.

          --
          Rory


          Comment

          • =?Utf-8?B?R2Vvcmdl?=

            #6
            Re: Cannot replace double quotes

            Hi Rory,

            Thanks for the reply.
            This still generates the same error message:
            "Data at the root level is invalid. Line 1, position 1."

            Here is how I added it to the code:
            -------------------------------------------------
            'Get xml from vendor
            sPaymentXML = Request.Form("P aymentXML")
            sPaymentXML =
            HttpContext.Cur rent.Server.Htm lDecode(Request .Form("PaymentX ML"))

            Dim xmldoc As XmlDocument = New XmlDocument
            xmldoc.LoadXml( sPaymentXML)
            -------------------------------------------------

            I did have to change from "System.Web.Htt pServerUtility. HtmlDecode" to
            "HttpContext.Cu rrent.Server.Ht mlDecode" because when I used the first one I
            was getting a message that "Reference to a non-shared member requires an
            object reference." But once I changed that, there was no problem with that
            line of code, I'm just not sure that it still created the XML as LoadXML() is
            looking for.



            "Rory Becker" wrote:
            Hello george,
            >
            'Get xml from vendor
            sPaymentXML = Request.Form("P aymentXML")
            >
            Try...
            -------------------------------------------------------------
            sPaymentXML = System.Web.Http ServerUtility.H tmlDecode(Reque st.Form("Paymen tXML"))
            -------------------------------------------------------------
            >
            I think the vendor or some intermediate process might have HtmlEncoded your
            XML.
            >
            --
            Rory

            Comment

            • =?Utf-8?B?R2Vvcmdl?=

              #7
              Re: Cannot replace double quotes

              I should say though, that the new code you mentioned:
              sPaymentXML =
              System.Web.Http ServerUtility.H tmlDecode(Reque st.Form("Paymen tXML"))

              does work when I am creating the temp file and loading it using
              xmlDoc.Load(). It just doesn't help with the xmldoc.LoadXML( ) function.

              "George" wrote:
              Hi Rory,
              >
              Thanks for the reply.
              This still generates the same error message:
              "Data at the root level is invalid. Line 1, position 1."
              >
              Here is how I added it to the code:
              -------------------------------------------------
              'Get xml from vendor
              sPaymentXML = Request.Form("P aymentXML")
              sPaymentXML =
              HttpContext.Cur rent.Server.Htm lDecode(Request .Form("PaymentX ML"))
              >
              Dim xmldoc As XmlDocument = New XmlDocument
              xmldoc.LoadXml( sPaymentXML)
              -------------------------------------------------
              >
              I did have to change from "System.Web.Htt pServerUtility. HtmlDecode" to
              "HttpContext.Cu rrent.Server.Ht mlDecode" because when I used the first one I
              was getting a message that "Reference to a non-shared member requires an
              object reference." But once I changed that, there was no problem with that
              line of code, I'm just not sure that it still created the XML as LoadXML() is
              looking for.
              >
              >
              >
              "Rory Becker" wrote:
              >
              Hello george,
              >'Get xml from vendor
              >sPaymentXML = Request.Form("P aymentXML")
              Try...
              -------------------------------------------------------------
              sPaymentXML = System.Web.Http ServerUtility.H tmlDecode(Reque st.Form("Paymen tXML"))
              -------------------------------------------------------------

              I think the vendor or some intermediate process might have HtmlEncoded your
              XML.

              --
              Rory

              Comment

              Working...