473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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("PaymentXML").ToString

'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.Replace("""", "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??
Jun 27 '08 #1
6 3072
Hello george,

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

http://blog.madskristensen.dk/post/D...s-invalid.aspx
--
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("PaymentXML").ToString
'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.Replace("""", "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.

Jun 27 '08 #2
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("PaymentXML") 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("PaymentXML") 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("PaymentXML")

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

'Create temp file
Dim sTempFileName As String = System.IO.Path.GetTempFileName()
Dim fstreamTemp As New System.IO.FileStream(sTempFileName,
IO.FileMode.Create, IO.FileAccess.ReadWrite)
Dim fwriterTemp As New IO.StreamWriter(fstreamTemp)

'Write the XMl to the temp file
With fwriterTemp
Try
.BaseStream.Seek(0, IO.SeekOrigin.End)
.WriteLine(sPaymentXML)
Finally
.Close()
End Try
End With

fstreamTemp.Close()

Dim xmlDoc As XmlDocument = New XmlDocument
xmlDoc.Load(sTempFileName)
===================================

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...

http://blog.madskristensen.dk/post/D...s-invalid.aspx
--
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("PaymentXML").ToString
'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.Replace("""", "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.


Jun 27 '08 #3
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("PaymentXML") 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("PaymentXML") 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("PaymentXML")

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

'Create temp file
Dim sTempFileName As String = System.IO.Path.GetTempFileName()
Dim fstreamTemp As New System.IO.FileStream(sTempFileName,
IO.FileMode.Create, IO.FileAccess.ReadWrite)
Dim fwriterTemp As New IO.StreamWriter(fstreamTemp)

'Write the XMl to the temp file
With fwriterTemp
Try
.BaseStream.Seek(0, IO.SeekOrigin.End)
.WriteLine(sPaymentXML)
Finally
.Close()
End Try
End With

fstreamTemp.Close()

Dim xmlDoc As XmlDocument = New XmlDocument
xmlDoc.Load(sTempFileName)
===================================

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...

http://blog.madskristensen.dk/post/D...s-invalid.aspx
--
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("PaymentXML").ToString
'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.Replace("""", "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.
Jun 27 '08 #4
Hello george,
>'Get xml from vendor
sPaymentXML = Request.Form("PaymentXML")
Try...
-------------------------------------------------------------
sPaymentXML = System.Web.HttpServerUtility.HtmlDecode(Request.Fo rm("PaymentXML"))
-------------------------------------------------------------

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

--
Rory
Jun 27 '08 #5
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("PaymentXML")
sPaymentXML =
HttpContext.Current.Server.HtmlDecode(Request.Form ("PaymentXML"))

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

I did have to change from "System.Web.HttpServerUtility.HtmlDecode" to
"HttpContext.Current.Server.HtmlDecode" 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("PaymentXML")

Try...
-------------------------------------------------------------
sPaymentXML = System.Web.HttpServerUtility.HtmlDecode(Request.Fo rm("PaymentXML"))
-------------------------------------------------------------

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

--
Rory
Jun 27 '08 #6
I should say though, that the new code you mentioned:
sPaymentXML =
System.Web.HttpServerUtility.HtmlDecode(Request.Fo rm("PaymentXML"))

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("PaymentXML")
sPaymentXML =
HttpContext.Current.Server.HtmlDecode(Request.Form ("PaymentXML"))

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

I did have to change from "System.Web.HttpServerUtility.HtmlDecode" to
"HttpContext.Current.Server.HtmlDecode" 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("PaymentXML")
Try...
-------------------------------------------------------------
sPaymentXML = System.Web.HttpServerUtility.HtmlDecode(Request.Fo rm("PaymentXML"))
-------------------------------------------------------------

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

--
Rory
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: middletree | last post by:
What's wrong with this code? strLongDesc = Replace(Replace(Replace(Replace(Trim(Request.Form("LongDesc")),"'","''"),vbC rLf,"<br>"),"<",&lt;),"<",&gt;) Background: This field is a textarea, and I...
2
by: P1ayboy | last post by:
I have a string that contains double quotes. I need to replace these, but how can i do it as the code would be: replace(string,""","") Is there a way of breaking out of the double quotes? ...
3
by: Christopher Benson-Manica | last post by:
Why doesn't this replace all double quotes in a string with the letter Q? source.replace( /"(*)"/g, "Q$1Q" ); -- Christopher Benson-Manica | I *should* know what I'm talking about - if I...
6
by: G. | last post by:
This is an obvious bug in the String.Replace function: //load a XML string into a document XmlDocument doc = new XmlDocument(); doc.LoadXml("<test id='' />"); //Obtain the string...
4
by: Neo Geshel | last post by:
Greetings I am using VB in my ASP.NET project that uses an admin web site to populate a database that provides content for a front end web site. I am looking for a way to use replace() to...
7
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem...
4
by: Kevin Thomas | last post by:
Hi there, If I have a string var, strFoo that contains double-quotes such that it looks like this: I "love" VB What do I pass into the "replace" method to replace the double-quotes with...
15
by: Jofio | last post by:
I have 40 or jpeg files which I want them displayed in a frame called "main" upon clicking thumbnails of the same pics in a "leftMenu" frame. The thumbnail jpeg files are called thumb1, thumb2,...
4
by: AZNewsh | last post by:
I am storing HTML in an oracle database, this is loaded from a textbox in a webpage, I convert ' to ' ' using the code below: foo.Replace("'", "''").Replace("&", "'||'&'||'") this works just...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.