parsing XML special characters in Javascript

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.
CallConnectionAsked:
Who is Participating?
 
JimBrandleyCommented:
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
 
REA_ANDREWCommented:
Have you set the encoding when you made your xml document?

Andrew
0
 
CallConnectionAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Jim
0
 
CallConnectionAuthor Commented:
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
 
CallConnectionAuthor Commented:


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
 
JimBrandleyCommented:
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
 
CallConnectionAuthor Commented:
Hi Jim,

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

Dmitri
0
 
JimBrandleyCommented:
Good luck.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.