.NET Framework Developer Center >
.NET Development Forums
>
ASMX Web Services and XML Serialization
>
Unexpected element: CDATA
Unexpected element: CDATA
- Hi,I have a an xsd file got it from a third party so i :- convert it to a C# classes using xsd.exe tool.- create a new instance from one of the classes.- serialize it to an xml document.- convert it to a string .- encrypt it(as the the third party want).- pass the encrypted message string to a web service(as one parameter).Then i got the following exception:org.apache.xmlbeans.XmlException: error: Unexpected element: CDATA (Java Exception)When we trace the third party logs every thing went well except a CDATA element appear in front of the decrypted xml document , this is why the above exception thrown.From where this CDATA came ihave no idea,so i have searched the web to find a possible reason but no luck some say remove every space or new line but it didn't work.Any help
Matelin
Answers
- It obviously appeared because your code created it. Go find out what your code did.
John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects
- Unproposed As Answer byMatelin Wednesday, November 04, 2009 12:13 PM
- Proposed As Answer byAmadeo Casas - MSFTModeratorWednesday, October 28, 2009 10:19 PM
- Marked As Answer byMatelin Thursday, November 05, 2009 5:17 PM
- Hi, I found the solution,when serializing an object I use the following :It return an XML as string,and when i try to convert it to an xml document i got the following exception :
String XmlizedString = null; MemoryStream memoryStream = new MemoryStream(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyType));<br/>XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,UTF8Encoding.UTF8); xmlSerializer .Serialize(xmlTextWriter, MyObject); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()); return XmlizedString;
Data at the root level is invalid. Line 1, position 1.I searched the net, and found the following link :http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/661369bc-2a22-4eb2-b7c9-2b8ac8f1b2cdIt got the solution ,the forth line should beXmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, new UTF8Encoding());
This exception killed me but finally solved :)
Matelin- Marked As Answer byMatelin Thursday, November 05, 2009 5:16 PM
All Replies
- It obviously appeared because your code created it. Go find out what your code did.
John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects
- Unproposed As Answer byMatelin Wednesday, November 04, 2009 12:13 PM
- Proposed As Answer byAmadeo Casas - MSFTModeratorWednesday, October 28, 2009 10:19 PM
- Marked As Answer byMatelin Thursday, November 05, 2009 5:17 PM
- Hi, I found the solution,when serializing an object I use the following :It return an XML as string,and when i try to convert it to an xml document i got the following exception :
String XmlizedString = null; MemoryStream memoryStream = new MemoryStream(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyType));<br/>XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,UTF8Encoding.UTF8); xmlSerializer .Serialize(xmlTextWriter, MyObject); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()); return XmlizedString;
Data at the root level is invalid. Line 1, position 1.I searched the net, and found the following link :http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/661369bc-2a22-4eb2-b7c9-2b8ac8f1b2cdIt got the solution ,the forth line should beXmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, new UTF8Encoding());
This exception killed me but finally solved :)
Matelin- Marked As Answer byMatelin Thursday, November 05, 2009 5:16 PM
- Actuallly, if anything, the line should be:
XmlWriter xmlTextwriter = XmlWriter.Create(memoryStream);
John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects


