Ok here is my problem. I am trying to take a screenshot, add it to an xmldocument, send it over a socket, and read it with a XmlReader.
Here's some code...
Server Side
private void SendRandomData(object data)
{
XMLShitSock sock = data as XMLShitSock;
if(sock != null)
{
int incnum = 0;
while(sock.Connected)
{
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode productsNode = doc.CreateElement("image");
productsNode.InnerText = Convert.ToBase64String(Program.CaptureImageToBytes(new Point(0, 0), new Rectangle(0, 0, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height), ImageFormat.Png));
doc.AppendChild(productsNode);
string s = XMLShitSock.GetXmlString(doc);
doc.Save("temp.xml");
sock.WriteXMLMessage(doc);
incnum++;
Thread.Sleep(5000);
}
}
}
sock.WriteXMLMessage
public bool WriteXMLMessage(XmlDocument doc)
{
try
{
ShittySocket.Client.Send(Encoding.ASCII.GetBytes(GetXmlString(doc)));
return true;
}
catch(SocketException se)
{
this.Close();
return false;
}
}
To Read input
private void doInput()
{
MemoryStream ms = new MemoryStream();
NetworkStream ns = new NetworkStream(ShittySocket.Client, false);
while(_connected)
{
if(ns.DataAvailable)
{
StreamReader sr = new StreamReader(ns);
char[] b = new char[512];
int nread = sr.Read(b, 0, 512);
ms.Write(System.Text.Encoding.ASCII.GetBytes(b, 0, nread), 0, nread);
ms.Seek(0, System.IO.SeekOrigin.Begin);
XmlReaderSettings xrs = new XmlReaderSettings();
XmlReader reader = XmlReader.Create(ms);
if(reader.Read())
{
XmlDocument objXmlDocument = new XmlDocument();
objXmlDocument.Load(reader);
onInput(this, objXmlDocument);
ms.Close();
ms = new MemoryStream();
}
}
Thread.Sleep(100);
}
}
Problem I'm having is I get an error in doInput() saying the end of the document was reached without find the tag. I saved the xml file and looked through it and it exists, and I didn't even find any < characters besides those in the actual tags, so I'm not sure what's going on here. I'm obviously missing something.
Also, if you have concerns with the semantics or coding style, or anything that doesn't actually answer the question, please leave it as a comment, since it's not an answer.
Also I've look through this ->> http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.readbase64.aspx and I'm hoping that there is a better way to deal with base64 data than separating it from the XMLDocument itself. Thanks.
It looks like you're assuming the entire data will be read in one 512-byte chunk. Therefore, if the actual data is, say, 513 bytes long then you won't get the closing angle bracket > at the end of the document, thus the XML parser fails.
I don't know exactly what type of network stream you're using but if it's plain vanilla sockets then you need some kind of delimiter to know when you've reached the end of one "send" and the start of the next, because vanilla sockets are just a pipeline of data. With one read you could be receiving half, one, two, three-and-a-bit actual blocks.
Handily, you have a delimiter in the form of the closing tag.