469,290 Members | 1,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

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 21486
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stefania Scott | last post: by
4 posts views Thread by Robert | last post: by
11 posts views Thread by jarod1701 | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.