473,598 Members | 3,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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??
Jun 27 '08 #1
6 3085
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("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.

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

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


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

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("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.
Jun 27 '08 #4
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
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("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
Jun 27 '08 #6
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
Jun 27 '08 #7

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

Similar topics

8
4501
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 needed to account for apostrophes, which I had already done, and replaced line breaks with html line breaks on my page which displays this stuff. That works fine. But then a user entered this
2
1765
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? thanks
3
28371
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 ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
6
22132
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 again...Converts my '' to ""...strange, but ok. String strXML = doc.OuterXml; //trying to convert back the single quotes to double quotes
4
3119
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 replace multiple entries of an object within a string. That is, if a user hit twice to create one new line, but just once to create another, I want both types ( & twice to create a new line (by creating a blank line between paragraphs). ...
7
20919
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 comes in when you copy a double quote from MS Word and paste it in the text box. What happens is the double quote becomes slanted (“) so my code above can't filter it. I tried to do this text= Replace(text, "““","'")
4
24348
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 something else? strFoo = strFoo.replace(???,"&quot;")
15
1476
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, thumb3, thumb4, ...., thumb40. These files are smaller versions (50pixelX50pixel) of the larger jpeg files called 1, 2, 3, 4, ..., 40 which I want them displayed. To display the hyperlinked thumbnails in the "leftMenu" frame I have it in a loop.
4
2207
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 fine, then when I retrieve the text from the database and display it in a webpage, I convert it back using: objdr.Item("foo").Replace("''", "'").Replace("'||'&'||'", "&")
0
7902
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8265
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6719
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3898
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2412
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.