?
Solved

parsing XML special characters in Javascript

Posted on 2007-07-26
11
Medium Priority
?
1,204 Views
Last Modified: 2007-10-18
Hello XML Experts!!

I am parsing XML webpage in javascript... The problem is that XML gets generated using hexadecimal notation for special charaters; for instance '+' is represented as "x002B", ' ' as "x0020" etc.
Can you please either:
a) Suggest the most efficient way to decode this? Please consider the fact that you may have several repeating characters in the string - "My (New) Brilliant C++ Total" would have 4 spaces and 2 pulsses and 2 different bracket characters.
b) Suggest C# (or VB) way to generate xml using .WriteXml() that would NOT generate hexadecimal characters?


B might get a bit tricky, but I think that B is MUCH, much better than A, because javascript might be quite slow to parse the output of a large page? I would also prefer C# if at all possible.
0
Comment
Question by:CallConnection
  • 4
  • 4
9 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19574355
Have you set the encoding when you made your xml document?

Andrew
0
 

Author Comment

by:CallConnection
ID: 19574895
Hi Andrew!!

Hmm.. No, I wasn't sure how..
I read the data from SQL then, I used the following to generate XML (in the "original" page):
 ...
  myTable.TableName = "GetDetails";
  myTable.WriteXml(Response.OutputStream, System.Data.XmlWriteMode.IgnoreSchema);
  ...

Is there a way to set it here?
Thanks!
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19575247
I think Andrew is right the XmlTextWriter can be constructed like this:
XmlTextWriter tw = new XmlTextWriter(path, System.Text.Encoding.UTF8);

Jim
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:CallConnection
ID: 19576027
Hi Jim,

I have seen this, however I am really looking for a web service approach, in which case I think I need to use WriteXml and Encoding is not one of the options as far as I could see (?)

Dmitri
0
 

Author Comment

by:CallConnection
ID: 19576285


Hi, I suspect I might need to use something like:

                using System.Xml.Serialization;
[...]
                XmlSerializer xml = new XmlSerializer(typeof(DataTable));
                XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
                xsn.Add(string.Empty, string.Empty);
                xml.Serialize(Response.OutputStream, dtRet, xsn);
                //xml.Serialize(Response.OutputStream, dtRet, xsn, "UTF8");
   - except the last line does quite not work if you add encoding to it...  please help!

Dmitri
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19577294
Sorry about the lag - had to go to a meeting. Is this the way you were extracting it from the data table; i.e. using serialize?
0
 
LVL 22

Accepted Solution

by:
JimBrandley earned 1500 total points
ID: 19577481
Dmitri - If myTable.WriteXml(Response.OutputStream, System.Data.XmlWriteMode.IgnoreSchema); is the cause of the problem, you could attempt this:
replace Response.OutputStream with a new MemoryStream. Then you can construct an XmlTextWriter that uses the memory stream as input, and accepts the System.Encoding you need. The XmlTextWriter output can then be placed into Response.OutputStream. You could use the same approach if you need to using the serializer.

Jim
0
 

Author Comment

by:CallConnection
ID: 19577690
Hi Jim,

Thank you, sounds very good - I will try this when I'm back at work!

Dmitri
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19577772
Good luck.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question