CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    [RESOLVED] A network stream of XML and XmlSerializer

    Good day,

    I have a problem which presents as follows: We have a network stream from a socket. This one undergoes several transformations (AES then Base64) and finally ends in plain text messages/packets formated as XML:

    Code:
    <ACK>0
       <CHECK>asdf</CHECK>
       <SOMETHING>asdf</SOMETHING>
    </ACK>
    So far so good. We have created several classes out of the schemas of the XML messages sent and would like to use the XmlSerializer to read/write them all from/to a stream. Problem: One XmlSerializer can only handle one type at a time, but we have several messages to parse and thus several generated classes out of them. The current approach is to read the first line (the root element) and then decide which type must be passed to a serializer. You can imagine how ugly this huge if statement is.

    I have tried several approaches: First we introduced a class hierarchy with the messages so we can always deserialize the base class (call it Message) and use the XmlInclude attribute. Problem: The generated XML is not compatible with the messages we receive:

    Code:
    <!-- Generated, given that Message is base class and AckMessage is derived -->
    <MESSAGE xsi:type="AckMessage">
       <!-- ... -->
    </MESSAGE>
    Code:
    <!-- What we are looking for -->
    <ACK>
       <!-- ... -->
    </ACK>
    We also tried to alter the output and both the input via another layer of Stream (derived from StreamReader) to create a mapping of the two formats mentioned above. Though that implementation basically works it is rather hackish and error-prone. CanDeserialize() from the XmlSerializer also drops out as it reads the stream empty, which is not very helpful on a network stream.

    Question summed down: Is there an XmlSerializer which can deserialize a varied list of types off a stream?
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  2. #2
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: A network stream of XML and XmlSerializer

    After some thinking, testing and researching we found a fix: The XmlReader can read from any stream without droping any data. Which means that one can easily extract the root element from the XML message without removing data from the Reader() so that the XmlSerializer can still use the reader to deserialize the object from it. Oh well...

    We then added some sort of object creation facility mapping Root tags to types so the generic code goes something like:

    Code:
    XmlReader reader = XmlReader.Create(myNetworkStream);
    
    reader.Read(); // Read first element, assuming its the root
    
    Type type = rootElementToTypeMappings[reader.Name];
    // create a serializer from it
    XmlSerializer serializer = new XmlSerializer(type);
    // reader is still in good shape for a Deserialize here
    object obj = serializer.Deserialize(reader);
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured