Ask a questionAsk a question
 

AnswerUnexpected element: CDATA

  • Sunday, October 18, 2009 3:56 PMMatelin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Monday, October 19, 2009 2:43 PMJohn SaundersMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Thursday, November 05, 2009 5:16 PMMatelin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi, I found the solution,

    when serializing an object I use the following :

    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;
    
    It return an XML as string,and when i try to convert it to an xml document i got the following exception :

    Data at the root level is invalid. Line 1, position 1.

    I searched the net, and found the following link :


    It got the solution ,the forth line should be 

    XmlTextWriter 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

  • Monday, October 19, 2009 2:43 PMJohn SaundersMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Thursday, November 05, 2009 5:16 PMMatelin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi, I found the solution,

    when serializing an object I use the following :

    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;
    
    It return an XML as string,and when i try to convert it to an xml document i got the following exception :

    Data at the root level is invalid. Line 1, position 1.

    I searched the net, and found the following link :


    It got the solution ,the forth line should be 

    XmlTextWriter 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
    •  
  • Thursday, November 05, 2009 7:57 PMJohn SaundersMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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