473,396 Members | 1,877 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,396 software developers and data experts.

Escape codes embedded in XML

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
5 6364
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: yawnmoth | last post by:
say i have a for loop that would iterate through every character and put a space between every 80th one, in effect forcing word wrap to occur. this can be implemented easily using a regular...
2
by: Daniel | last post by:
I'm working with strings that contain xml escape codes, such as '0' and need a way in python to unescape these back to their ascii representation, such as '&' but can't seem to find a python method...
2
by: Felix | last post by:
If I set a breakpoint in visual studio 2000 and viewed a local variable (in the "locals" panel), I would see something like: sql | " SELECT Operator.FirstName, .... now I am using visual...
1
by: m++ | last post by:
Hi there, I am looking for some C++(object oriented) source codes of applications designed for embedded systems. I am going to use them as benchmarks. I've faced with some of these applications in...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
18
by: Steve Litvack | last post by:
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...
1
by: marcvill | last post by:
I need to send printer specific escape codes a printer for a POS register. Can anyone tell me how to send these codes to a printer using VB .NET and the Win32 spooler functions? I have looked at the...
15
by: pkaeowic | last post by:
I am having a problem with the "escape" character \e. This code is in my Windows form KeyPress event. The compiler gives me "unrecognized escape sequence" even though this is documented in MSDN....
5
by: Anton81 | last post by:
Hi all! I used escape sequences to produce colour output, but a construct like print "%8s" % str_with_escape doesn't do the right thing. I suppose the padding counts the escape characters,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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,...

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.