XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 22nd, 2003, 07:06 AM
mitchell mitchell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Bournemouth, England
Posts: 28 mitchell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 23 sec
Reputation Power: 0
Send a message via ICQ to mitchell Send a message via MSN to mitchell
Control Characters in XML and Soap

Problem:

I have a character string that is data received from a third party which contains control characters (specifically Ox19) within it.

The character can appear at any point and I cannot replace it with another character without invalidating the data completely.

XML 1.0 considers control characters as invalid and throws an error on reaching the character.

Can anyone suggest ANY sort of fix for this problem? Even some sort of nasty botch would be acceptable!!!

Thanks
Mitchell x

Reply With Quote
  #2  
Old September 22nd, 2003, 05:47 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Well, I suppose it depends on what you're doing with the string. Can you wrap it in <![CDATA[ ]]> tags?

Reply With Quote
  #3  
Old September 23rd, 2003, 06:00 AM
mitchell mitchell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Bournemouth, England
Posts: 28 mitchell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 23 sec
Reputation Power: 0
Send a message via ICQ to mitchell Send a message via MSN to mitchell
Control Characters in XML

Thanks for the reply

Great idea, tried it...., didn't work

As far as what I'm doing with the data is concerned...

I am generating a soap/xml document by serialising an object, this actually works fine and doesn't complain about the invalid Ox19 character. When I try to deserialise the soap/xml back into the object it fails while simply trying to parse the file because XML version 1.0 doesn't allow control characters! Flippin thing!

I'm going to try posting on the C# forum as well to see if there is a way round this problem that way but any further suggestions would be most welcome

Thanks again
mitchell x

Reply With Quote
  #4  
Old September 23rd, 2003, 12:00 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Well, I'm at a loss. One thing you might try is to pass values instead of the whole serialized object. It's harder, but since serializing doesn't seem to be working...

Reply With Quote
  #5  
Old November 18th, 2003, 05:09 AM
mdebruin mdebruin is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: London, UK
Posts: 1 mdebruin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Soap Extension

Hi mitchell,

I was running into this exact problem. When .NET sends soap requests, the xml isn't validated. The back-end of our project handles these invalid characters without a problem, and subsequent response messages included these invalid characters which makes .NET barf.

The only consistent way that I found to work around this problem is to write a soapextension that intercepts each message before it's deserialized. There's lots of articles out there that tell you how to create a soapextension, so I won't go into how soapextensions work here... but the guts of it is in the ProcessMessage method.

public override void ProcessMessage(SoapMessage message) {
MemoryStream reallyNewStream;
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
newStream.Position = 0;
// copy the new stream (which has the serialized message) into the oldstream so that the thing will be processed
CopyStream(newStream, oldStream);
break;
case SoapMessageStage.BeforeDeserialize:
// copy the old stream (which has the serialized message) into the newstream so that we can parse it...
CopyStream(oldStream, newStream);
reallyNewStream = new MemoryStream();
// replace all invalid chars in the soap response
ReplaceInvalidChars(newStream, reallyNewStream);
// copy the new, valid soap response into the newStream object so it can continue on it's merry way
CopyStream(reallyNewStream, newStream);
newStream.Position=0;
break;
case SoapMessageStage.AfterDeserialize:
break;
default:
throw new Exception("invalid stage");
}
}

The main problem with this is the fact that you have to go through each character on each and every soap request. Not pretty, but it works. I've been posting on different forums, and I haven't found a more elegant way of doing it.

Hope this helps.

Mike

Reply With Quote
  #6  
Old November 19th, 2003, 08:31 AM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Another idea occured to me. Base64 or uuencode the serialized object before transmission. Then decode it on the other side. It means more work on each end of the transmission but it should handle all exception cases and be fairly portable.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Control Characters in XML and Soap


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT