473,403 Members | 2,222 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,403 software developers and data experts.

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("<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
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 22072
"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("<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
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("<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
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("<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
String strNewXML = strXML.Replace("\"","'");

Console.WriteLine (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.com>
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("<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
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("<test 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("<test id='' />");
//Obtain the string again...Converts my '' to ""...strange, 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
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...
6
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...
3
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...
12
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(" &...
4
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...
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...
16
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...
11
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...
5
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...
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: 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
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
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
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,...
0
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...

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.