473,786 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bug with String Replace with /"/" or double quotes

G.
This is an obvious bug in the String.Replace function:

//load a XML string into a document
XmlDocument doc = new XmlDocument();
doc.LoadXml("<t est id='' />");

//Obtain the string again...Convert s my '' to ""...strang e, but ok.
String strXML = doc.OuterXml;

//trying to convert back the single quotes to double quotes
String strNewXML = strXML.Replace( "\"","'");
//BUG the resulting string is : <test id=" />
//The Replace string with "" is scrambled to a single "

Can anyone explain how I can work around this???
StringBuilder does the same thing.

I need to convert "" to '' in the string.

Hard to believe they have basic bugs like this...
no wonder no one is using .NET.
Nov 16 '05 #1
6 22211
"to************ *@hotmail.com" flamed:
This is an obvious bug in the String.Replace function:

//load a XML string into a document
XmlDocument doc = new XmlDocument();
doc.LoadXml("<t est id='' />");

//Obtain the string again...Convert s my '' to ""...strang e, but ok.
String strXML = doc.OuterXml;

//trying to convert back the single quotes to double quotes
String strNewXML = strXML.Replace( "\"","'");

//BUG the resulting string is : <test id=" />
//The Replace string with "" is scrambled to a single "

Can anyone explain how I can work around this???
StringBuilder does the same thing.

I need to convert "" to '' in the string.

Hard to believe they have basic bugs like this...
no wonder no one is using .NET.


Looks like PEBCAC to me, biker boy. I get "<test id='' />", as
expected.

--

www.midnightbeach.com
Nov 16 '05 #2
G. <to************ *@hotmail.com> wrote:
This is an obvious bug in the String.Replace function:
I suspect not, to be honest... I suspect you've got problems elsewhere.
//load a XML string into a document
XmlDocument doc = new XmlDocument();
doc.LoadXml("<t est id='' />");

//Obtain the string again...Convert s my '' to ""...strang e, but ok.
String strXML = doc.OuterXml;

//trying to convert back the single quotes to double quotes
String strNewXML = strXML.Replace( "\"","'");
//BUG the resulting string is : <test id=" />
//The Replace string with "" is scrambled to a single "

Can anyone explain how I can work around this???
StringBuilder does the same thing.

I need to convert "" to '' in the string.
I can't reproduce the problem. Here's a short program which given your
code above really should show it:

using System;
using System.Xml;

class Test
{
static void Main()
{
//load a XML string into a document
XmlDocument doc = new XmlDocument();
doc.LoadXml("<t est id='' />");

//Obtain the string again...Convert s my '' to ""...strang e, but ok.
String strXML = doc.OuterXml;

//trying to convert back the single quotes to double quotes
String strNewXML = strXML.Replace( "\"","'");

Console.WriteLi ne (strNewXML);
}
}

Output:
<test id='' />

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
Hard to believe they have basic bugs like this...
no wonder no one is using .NET.


Apart from the many people who *are* using it, of course...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
G.,

I know of two ways around this. One leave your code the same and use a
verbatim string when you load the XML. You can use this by placing an @
symbol at the beginning of the string.

For example:
doc.LoadXml(@"< test id='' />");

Second if you are going to write out the document you can use the QuoteChar
property on the XmlTextWriter to set the character to single or double quote.
The default is double quote. I have provided a link for you below that
details this.

Hope this helps.
-------------------------

http://msdn.microsoft.com/library/de...asp?frame=true
Nov 16 '05 #4
G,

After re-examining what you are asking I think that the first solution that
I have provided is not applicable to your question but the second is. Could
it be that you are looking at the string in the locals window and it looks
like a single double quote?

"Brian Brown" wrote:
G.,

I know of two ways around this. One leave your code the same and use a
verbatim string when you load the XML. You can use this by placing an @
symbol at the beginning of the string.

For example:
doc.LoadXml(@"< test id='' />");

Second if you are going to write out the document you can use the QuoteChar
property on the XmlTextWriter to set the character to single or double quote.
The default is double quote. I have provided a link for you below that
details this.

Hope this helps.
-------------------------

http://msdn.microsoft.com/library/de...asp?frame=true

Nov 16 '05 #5
On 28 Jan 2005 09:03:54 -0800, G. wrote:
This is an obvious bug in the String.Replace function:

//load a XML string into a document
XmlDocument doc = new XmlDocument();
doc.LoadXml("<t est id='' />");

//Obtain the string again...Convert s my '' to ""...strang e, but ok.
String strXML = doc.OuterXml;

//trying to convert back the single quotes to double quotes
String strNewXML = strXML.Replace( "\"","'");

//BUG the resulting string is : <test id=" />
//The Replace string with "" is scrambled to a single "
Can't replicate here. In this instance for me strNewXML is
"<test id='' />" just as I would expect.
Can anyone explain how I can work around this???
StringBuilder does the same thing.

I need to convert "" to '' in the string.
Using replace works here.
Hard to believe they have basic bugs like this...
no wonder no one is using .NET.

Again, your bug is not reproducible here. Possibly the font being used
makes it unclear. Try the following instead:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<t est id='1' />");
string strXML = doc.OuterXml;
string strNewXML = strXML.Replace( "\"", "\'");
--
Tom Porterfield
Nov 16 '05 #6
//load a XML string into a document
XmlDocument doc = new XmlDocument();
doc.LoadXml("<t est id='' />");
//Obtain the string again...Convert s my '' to ""...strang e, but ok.
String strXML = doc.OuterXml;
This is not at all strange: it's one of two possibilities that I would
expect.

Reading a string as XML means building a DOM tree from it. The XML is
stored as a hierarchy of tokens and values in memory. Syntactic markers
such as <, >, =, and ' are discarded in the process.

When you ask for the OuterXml back again, the software reconstructs the
string from the DOM structure, at which point it inserts syntactic
markers again to create valid XML. The only requirement is that it
produce valid XML, not that it produce precisely what you gave it.
Double quotes are perfectly valid delimeters in XML, so it uses those.

True, some DOM represenations may give you back exactly what you gave
them, if they build their structures on top of the original data
stream, thus leaving every original character intact. I believe that
BEA does this in some of their products. However, a lot of DOM
implementations don't: they throw away the original text and keep only
the values that matter.

As for your Replace() method error, it looks like a PICNIC error to
me....

Nov 16 '05 #7

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

Similar topics

9
26170
by: PaulThomas | last post by:
I want to "look" through a comma delimited String and "take it apart" by finding the comma's and then put each "set of characters" into seperate strings - - - maybe an array, maybe seperate cells in a table -or- whatever... Can anyone help me locate the commands (samples) that might do this? Actually, I want to read a file and use these comma seperated groups of data to "load" a database and this comma seperated data is the individual...
6
13491
by: Jeff | last post by:
Hi, does anyone know why this: <a onclick="insertatcaret(window.opener.document.formname.fieldname,'<td class="header">')">text</a> returns a "Unterminated String Constant" error message in IE 6.0 but if I take out the double quotes around header it does not? I need double quotes around header, I thought this was possible in javascript so long as the pair of double quotes is surrounded by a pair of single quotes.
3
3170
by: Stefania Scott | last post by:
How do I resolve the problem of passing a string that has quotes within in a SQL statement? Sometimes the string contains a single quote (') and some others it contains the double quote (")? Any clue... Thank you, Stefania
12
9645
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34) & ", " & Chr(34) & CurrentEventDetails & ")" strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName & "</A><BR>" The problem I have is that when the string variables or contain a string with an...
4
2344
by: Robert | last post by:
Hello. I have tried to remove the char "\" from a string that I am building in codebehind. to be used in a script tag. I have tried adding (char)34 to the string instead of the escape character, as well as @then double quotes. when i watch the string in the command window or the watch window it has the escape character in the string allways \"
7
21024
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, "““","'")
16
5887
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array with the following elements arr(0) = "MyString" arr(1) = 40 arr(2) = "Hello world"
11
3665
by: jarod1701 | last post by:
Hi, i'm currently trying to replace an unknown string using regular expressions. For example I have: user_pref("network.proxy.http", "server1") What do I have to do to replace the "server1" part (which could be
5
5158
by: Hels Bells | last post by:
Hi, I'm looking to do some manipulation on a string containing html code in asp which will involve me either using some regular expressions or just plain old simple replace functionality. The text that I want to use will have double quotes in it (as it's xhtml code) so I can't set it as a string value and is actually brought into the page from a content managed element (the details of which I don't think I'll need to go into but...
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10164
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
9962
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
8992
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 project—planning, coding, testing, and deployment—without 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...
0
5398
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...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
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
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.