By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

Escape codes embedded in XML

P: n/a
Hello,

I have built an XMLDocument object instance and I get the following string
when I examine the InnerXml property:

<?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1
QID=\"55111\"><Tag2 AID=\"5511101\"></Tag2></Tag1><Tag1 QID=\"55112\"><Tag2
AID=\"5511217\"></Tag2></Tag1><Tag1 QID=\"5512282\"><Tag2
AID=\"551228206\"></Tag2></Tag1><Tag1 QID=\"55114\"><Tag2
AID=\"5511406\"></Tag2></Tag1><Tag1 QID=\"55115\"><Tag2
AID=\"5511505\"></Tag2></Tag1></UserData></ROOT>

Notice that the double quotes are all escaped--that is they appear as

\"

versus

"

I would like to "transform" or "convert" this string into a string in which
the escape characters are "converted". This would generate a string that
would appear like this:

<?xml version="1.0"?><ROOT><UserData UserID="2282"><Tag1 QID="55111"><Tag2
AID="5511101"></Tag2></Tag1><Tag1 QID="55112"><Tag2
AID="5511217"></Tag2></Tag1><Tag1 QID="5512282"><Tag2
AID="551228206"></Tag2></Tag1><Tag1 QID="55114"><Tag2
AID="5511406"></Tag2></Tag1><Tag1 QID="55115"><Tag2
AID="5511505"></Tag2></Tag1></UserData></ROOT>

I do not want to use a search/replace algorithm because I cannot be entirely
sure that there will not be escape sequences other than \" in the string. I
am actually seeking a solution that would "transform" the \" to " (as I
require) but also one that would convert \n to ASCII 10 and \r to ASCII 13.
In other words, I would like to convert the escape-encoded string to a
standard ASCII string.

Moreover, why does the XMLDocument's InnerXml return an escape-encoded
string instead of a "straight" (unencoded) string? Is there a way to get the
InnerXml to return the "straight" string? I plan to pass the XML string as
an argument to a Sql stored procedure; therefore, the encoded string does
not work--I must use the unencoded version. Certainly others have
encountered this problem--I just could not find a single solution and I
spend hours searching MSDN and Google! For example, I read some other
threads that suggest using ActiveXMessageFormatter, but not one of them
actually explained how to do it--and pseudocode can only be so useful...

Thank you in advance for any assistance. I will confirm whether any
suggestions work.

Regards,
Steve

Nov 11 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I don't believe the System.Xml.XmlDocument class should behave in the way
you describe. How are you constructing this XML and how are you displaying
it?
--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Steve Litvack" <us************@yahoo.com> wrote in message
news:lN*********************@twister.austin.rr.com ...
Hello,

I have built an XMLDocument object instance and I get the following string
when I examine the InnerXml property:

<?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1
QID=\"55111\"><Tag2 AID=\"5511101\"></Tag2></Tag1><Tag1 QID=\"55112\"><Tag2 AID=\"5511217\"></Tag2></Tag1><Tag1 QID=\"5512282\"><Tag2
AID=\"551228206\"></Tag2></Tag1><Tag1 QID=\"55114\"><Tag2
AID=\"5511406\"></Tag2></Tag1><Tag1 QID=\"55115\"><Tag2
AID=\"5511505\"></Tag2></Tag1></UserData></ROOT>

Notice that the double quotes are all escaped--that is they appear as

\"

versus

"

I would like to "transform" or "convert" this string into a string in which the escape characters are "converted". This would generate a string that
would appear like this:

<?xml version="1.0"?><ROOT><UserData UserID="2282"><Tag1 QID="55111"><Tag2
AID="5511101"></Tag2></Tag1><Tag1 QID="55112"><Tag2
AID="5511217"></Tag2></Tag1><Tag1 QID="5512282"><Tag2
AID="551228206"></Tag2></Tag1><Tag1 QID="55114"><Tag2
AID="5511406"></Tag2></Tag1><Tag1 QID="55115"><Tag2
AID="5511505"></Tag2></Tag1></UserData></ROOT>

I do not want to use a search/replace algorithm because I cannot be entirely sure that there will not be escape sequences other than \" in the string. I am actually seeking a solution that would "transform" the \" to " (as I
require) but also one that would convert \n to ASCII 10 and \r to ASCII 13. In other words, I would like to convert the escape-encoded string to a
standard ASCII string.

Moreover, why does the XMLDocument's InnerXml return an escape-encoded
string instead of a "straight" (unencoded) string? Is there a way to get the InnerXml to return the "straight" string? I plan to pass the XML string as
an argument to a Sql stored procedure; therefore, the encoded string does
not work--I must use the unencoded version. Certainly others have
encountered this problem--I just could not find a single solution and I
spend hours searching MSDN and Google! For example, I read some other
threads that suggest using ActiveXMessageFormatter, but not one of them
actually explained how to do it--and pseudocode can only be so useful...

Thank you in advance for any assistance. I will confirm whether any
suggestions work.

Regards,
Steve


Nov 11 '05 #2

P: n/a
Why

string s = Convert.ToString(xmlAnswers.InnerXml);

and not

string s = xmlAnswers.InnerXml;

--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Steve Litvack" <us************@yahoo.com> wrote in message
news:JC********************@twister.austin.rr.com. ..
Yes--I was surprised myself. Thank you--you may be able to help!

Here is my code (note that even the XML Declaration ends up with the escaped quotes--so it's not just limited to data coming from the ButtonList control embedded in a DataGrid):

In the code below, I build the XML Document and then set the result to
string s.
s then contains the XML text I showed--the text that contains the escape
codes...

See below, and thanks again.

--Steve

// save the survey answers as an XML Document (XMLAnswers)
XmlDocument xmlAnswers;
XmlNode xmlNode, xmlAttribute;
XmlElement xmlElem, xmlElem2, xmlElem3;
XmlText xmlText;

// <ROOT>
xmlAnswers = new XmlDocument();

// add the XML declaration section
xmlNode = xmlAnswers.CreateNode(XmlNodeType.XmlDeclaration," ","");
xmlAnswers.AppendChild(xmlNode);

// add the root element
xmlElem = xmlAnswers.CreateElement("","ROOT","");

// xmltext=xmldoc.CreateTextNode("This is the text of the root element");
xmlText = xmlAnswers.CreateTextNode("");
xmlElem.AppendChild(xmlText);
xmlAnswers.AppendChild(xmlElem);
// <PROFILE> tag
// add another element (child of the root)
xmlElem = xmlAnswers.CreateElement("","Profile","");

// xmltext=xmldoc.CreateTextNode("The text of the sample element");
xmlText = xmlAnswers.CreateTextNode("");
xmlElem.AppendChild(xmlText);

// <PROFILE UserID> attribute
xmlAttribute = xmlAnswers.CreateNode(XmlNodeType.Attribute,"UserI D","");
xmlAttribute.Value =
((SiteIdentity)Context.User.Identity).UserID.ToStr ing();
xmlElem.Attributes.Append((System.Xml.XmlAttribute )xmlAttribute);

// loop through questions
System.Web.UI.WebControls.DataGridItemCollection dgCollection =
this.dgrdQuestions.Items;
foreach (System.Web.UI.WebControls.DataGridItem dgItem in dgCollection)
{
// get the buttonlist object so that we can inspect the SelectedValue
System.Web.UI.WebControls.RadioButtonList ButtonList;
ButtonList =
(System.Web.UI.WebControls.RadioButtonList)(dgItem .FindControl("radlstAnswer s"));

// <QUESTION> tag
xmlElem2 = xmlAnswers.CreateElement("","Question","");
xmlText = xmlAnswers.CreateTextNode("");
xmlElem2.AppendChild(xmlText);

// <QUESTION PrQuID> attribute
xmlAttribute = xmlAnswers.CreateNode(XmlNodeType.Attribute,"PrQuI D",""); xmlAttribute.Value = dgItem.Cells[2].Text;
xmlElem2.Attributes.Append((System.Xml.XmlAttribut e)xmlAttribute);

// <ANSWER> tag
// add another element (child of the root)
xmlElem3 = xmlAnswers.CreateElement("","Answer","");

// xmltext=xmldoc.CreateTextNode("The text of the sample element");
xmlText = xmlAnswers.CreateTextNode("");
xmlElem3.AppendChild(xmlText);

// <ANSWER PrAnID> attribute
xmlAttribute = xmlAnswers.CreateNode(XmlNodeType.Attribute,"PrAnI D",""); xmlAttribute.Value = ButtonList.SelectedValue.ToString();
xmlElem3.Attributes.Append((System.Xml.XmlAttribut e)xmlAttribute);
xmlElem2.AppendChild(xmlElem3);

// add the question to the profile tag
xmlElem.AppendChild(xmlElem2);
// add the profile to the root tag
xmlAnswers.ChildNodes.Item(1).AppendChild(xmlElem) ;
}

string s = Convert.ToString(xmlAnswers.InnerXml);


"Dare Obasanjo [MSFT]" <da***@online.microsoft.com> wrote in message
news:uj*************@TK2MSFTNGP10.phx.gbl...
I don't believe the System.Xml.XmlDocument class should behave in the way you describe. How are you constructing this XML and how are you displaying it?


Nov 11 '05 #3

P: n/a
Hi:

That really was an oversight--I originally had the version you suggestion
("string s = xmlAnswers.InnerXml") but was experimenting with other ways of
getting a string result in an effort to try to avoid getting those pesky
escape codes. My way and your way are returning the same string.

Any other ideas?

Regards,
Steve

"Dare Obasanjo [MSFT]" <da***@online.microsoft.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Why

string s = Convert.ToString(xmlAnswers.InnerXml);

and not

string s = xmlAnswers.InnerXml;

Nov 11 '05 #4

P: n/a
Steve Litvack wrote:
Any other ideas?

I can't reproduce the problem. Actually to be able to compile you code I had
to comment out foreach loop, which gives me only
<?xml version="1.0"?><ROOT></ROOT>
But even that doesn't have nothing pesky.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #5

P: n/a
I feel like a darn fool...

You're right: I was trying to debug Sql (the root cause was an XML case
issue), and used the watch window to observe my XML string. I have a VB
background, and in VB 6 and below you would never see a "VB representation"
of a string that does not match the actual output for the string (at least I
cannot think of an example; of course, an embedded low ASCII char, such as
Chr(10)--a linefeed char--could show a funky character when in a watch
window, but it would not be "escaped"). So C#'s representation of the string
IN THE WATCH WINDOW is what threw me because it shows the escape sequences!

I outputted the value with Response.Write(), and see that the text that
appears does, in fact, not have the escape sequences.

So, all along this was a red herring for me (XML case was the root cause).

Thanks to all for setting me straight.

Regards,
Steve
"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
Steve Litvack wrote:
Any other ideas? I can't reproduce the problem. Actually to be able to compile you code I

had to comment out foreach loop, which gives me only
<?xml version="1.0"?><ROOT></ROOT>
But even that doesn't have nothing pesky.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.