简体   繁体   中英

Reading Xml over Socket c#

Im trying to read/write xml from/to a server using a tcp socket connection. It gives me the error Root element is missing. I know there are xmpp/jabber libraries out there but I am looking for an easy custom solution how to do this in general and how to fix the error in my code. I would greatly appreciate if someone could help me out :)

        private void WritePreauthStreamInit(XmlWriter writer, string domain)
        {               
            this.socketStream = new NetworkStream(this._socket);

            XmlWriter writer1 = XmlWriter.Create(this.socketStream);
            writer1.WriteStartElement("stream", "stream", "http://etherx.jabber.org/streams");
            writer1.WriteAttributeString("to", domain);
            writer1.WriteAttributeString("version", "1.0");
            writer1.WriteStartElement("start-ack");
            writer1.WriteEndElement();
            writer1.Flush();

            XmlReader reader1 = XmlReader.Create(this.socketStream);
                while (reader1.Read()) //Exception: Root element is missing
                {
                    string xml = reader1.Read().ToString();
                }
        }

My guess is that either XmlReader has false assumptions regarding buffer size of received data (IE: it expects to read either 4096 bytes or hit the end of the stream before it starts processing data) or the server is not sending what would be a valid document.

Either way, you could check by implementing a logging stream wrapper which dumps network traffic to disk or by using Wireshark or similar.

public class LoggingStreamWrapper : Stream
{
    private Stream baseStream;
    private TextWriter twLog;
    private Encoding baseEncoding;

    public LoggingStreamWrapper(Stream baseStream, TextWriter twLog)
        : this(baseStream, twLog, Encoding.UTF8)
    {
    }

    public LoggingStreamWrapper(Stream baseStream, TextWriter twLog, Encoding encoding)
    {
        if (baseStream == null)
            throw new ArgumentNullException("baseStream");
        if (twLog == null)
            throw new ArgumentNullException("twLog");

        this.baseStream = baseStream;
        this.twLog = twLog;
        this.baseEncoding = encoding;
    }

    public override bool CanRead
    {
        get { return baseStream.CanRead; }
    }

    public override bool CanSeek
    {
        get { return baseStream.CanSeek; }
    }

    public override bool CanWrite
    {
        get { return baseStream.CanWrite; }
    }

    public override void Flush()
    {
        baseStream.Flush();
        twLog.WriteLine("Flushed stream");
        twLog.Flush();
    }

    public override long Length
    {
        get { return baseStream.Length; }
    }

    public override long Position
    {
        get { return baseStream.Position; }
        set
        {
            baseStream.Position = value;

            twLog.WriteLine(string.Format("Set position to {0}", value));
            twLog.Flush();
        }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        var bRead = baseStream.Read(buffer, offset, count);

        if (bRead > 1)
        {
            twLog.WriteLine(string.Format("Read {0} bytes from stream: {1}\r\n{2}", bRead,
                getText(buffer, offset, bRead),
                Convert.ToBase64String(buffer, offset, bRead, Base64FormattingOptions.InsertLineBreaks)));
            twLog.Flush();
        }
        else
        {
            twLog.WriteLine(string.Format("Read {0} bytes from stream", bRead));
            twLog.Flush();
        }

        return bRead;
    }

    private string getText(byte[] buffer, int offset, int bRead)
    {
        try
        {
            return baseEncoding.GetString(buffer, offset, bRead);
        }
        catch
        {
            return "{ERROR: Could not convert to text}";
        }
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        var newpos = baseStream.Seek(offset, origin);

        twLog.WriteLine(string.Format("Seeked to {0} relative to {1}.  New offset {2}", offset, origin, newpos));
        twLog.Flush();

        return newpos;
    }

    public override void SetLength(long value)
    {
        baseStream.SetLength(value);

        twLog.WriteLine(string.Format("Set length to {0}", value));
        twLog.Flush();
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        baseStream.Write(buffer, offset, count);

        twLog.WriteLine(string.Format("Wrote {0} bytes to stream: {1}\r\n{2}", count,
            getText(buffer, offset, count),
            Convert.ToBase64String(buffer, offset, count, Base64FormattingOptions.InsertLineBreaks)));
        twLog.Flush();
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        if (disposing)
        {
            twLog.Dispose();
            baseStream.Dispose();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM