C# XmlReader XmlException with multiple XML Declarations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gpet44
    New Member
    • Mar 2008
    • 1

    C# XmlReader XmlException with multiple XML Declarations

    Hi,

    I have a problem receiving XML over a NetworkStream in C# .Net 2.0/3.0. I'm creating an XMLReader from the stream. I get the following exception when the XmlReaderreads from the stream as follows while(reader.Re ad()) { ... }
    System.Xml.XmlE xception: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 3.

    Unfortunately I have no control over the client-side application and I have to deal with it as is. It sends either 1 or 2 XML documents at a time over the network, and each document contains a xml declaration. When the client sends 1 at a time everything works normally, when 2 documents are sent together the second document throws the exception.
    I've tried several different approaches using XmlReaderSettin gs ~ ConformanceLeve l.FRAGMENT and IgnoreProcessin gInstructions. I tried to use the deprecated XmlTextReader.R eset() method, these approaches still throw the exception. I also tried catching the exception and continuing, and also creating a new XmlReader as soon as the first document is read, both of these approaches result in the first document being read properly and the second being skipped entirely.

    Does anyone have any ideas ?

    Thanks!

    The relevant parts of my server-side code are as follows :
    Code:
    private void ListenForClients()
            {
                running = true;
                try
                {
                    this.serverSocket.Start();
    
                    while (running)
                    {
                        log.Debug("Waiting for Connection..");
                        //blocks until a client has connected to the server
                        TcpClient client = this.serverSocket.AcceptTcpClient();
    
                        //create a thread to handle communication
                        //with connected client
                        Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
    
                        clients.Add(client);
                        clientThread.Start(client);
                    }
                }
                catch (SocketException e)
                {
                    // This is called when Socket is Stopped
                    log.Debug("Socket Stopped", e);
                }
                catch (Exception e)
                {
                    log.Error("ListenerThread Exception", e);
                }
            }
    
            private void HandleClientComm(object client)
            {
                TcpClient tcpClient = (TcpClient)client;
    
                log.Info("Client Accepted: " + tcpClient.Client.RemoteEndPoint);
               
                try
                {
                    NetworkStream stream = tcpClient.GetStream();
    
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.ValidationType = ValidationType.Schema;
                    settings.ConformanceLevel = ConformanceLevel.Auto;
                    settings.IgnoreWhitespace = true;
                  
                    XmlReader reader = XmlReader.Create(stream, settings);
                   
                    // Break down nodes
                    StringBuilder buff = new StringBuilder();
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                bool isEmpty = reader.IsEmptyElement;
                                buff.Append("<" + reader.Name);
    
                                // Read attributes.
                                while (reader.MoveToNextAttribute())
                                {
                                    buff.Append(" " + reader.Name + "='" + reader.Value + "'");
                                }
                                if (isEmpty)
                                {
                                    buff.Append("/>");
                                }
                                else
                                {
                                    buff.Append(">");
                                }
                                break;
                            case XmlNodeType.Text:
                                buff.Append(reader.Value);
    
                                break;
                            case XmlNodeType.EndElement:
                                buff.Append("</" + reader.Name + ">");
                                if (reader.Depth.Equals(0))
                                {
                                    // On end tag - with depth 0 (root element) Print XML
                                    Console.WriteLine(buff.ToString());
                                    buff.Remove(0, buff.ToString().Length);                              
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
                catch (SocketException e)
                {
                    log.Error("SocketException:", e);
                }
                catch (IOException e)
                {
                    log.Info("Client disconnect detected "+e.Message);
                }
                catch (Exception e)
                {
                    log.Error("Exception: ", e);
                    if (e.InnerException != null)
                    {
                        log.Error("Exception: ", e.InnerException);
                    }
                }
                finally
                {
                    tcpClient.Close();
                    tcpClient = null;
                }
            }
Working...