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

  • Thread starter Thread starter G.
  • Start date Start date
G

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.
 
"(e-mail address removed)" 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.
 
G. said:
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...
 
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.
 
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?
 
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
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("\"", "\'");
 
//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....
 
Back
Top