c# - xml string root element missing exception /tcp/ip listener (code and string attached) -
c# - xml string root element missing exception /tcp/ip listener (code and string attached) -
hello have read 15 different sites error yet cant prepare blame lack of xml parsing /streaming , reading experience if can help me prepare problem win fluffy kitten mind command powers
// le tcp ip listener code
requestcount = requestcount + 1; networkstream networkstream = clientsocket.getstream(); byte[] bytesfrom = new byte[100025]; networkstream.read(bytesfrom, 0, clientsocket.receivebuffersize); string datafromclient = system.text.encoding.ascii.getstring(bytesfrom); datafromclient = datafromclient.substring(0, datafromclient.indexof("what devil must utilize here tried <monkey> fails"));
//le xml string
<?xml version="1.0" encoding="utf-8"?> <monkey> <chicken> <cow>brown</cow> <cowid>123mooo</cowid> </chicken> </monkey>
//parse , upload info db
var monkey= xdocument.parse(datafromclient) .descendants("monkey") .select(n => new { amount = n.element("cow").value, tellno = n.element("cowid").value }); foreach (var item in monkey) { mysqlcommand cmd = new mysqlcommand("stringlistenerupdate", conn); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.add(new mysqlparameter("cowtype", item.cow)); cmd.parameters.add(new mysqlparameter("cowuniqueid", item.cowid)); cmd.executenonquery(); conn.close(); console.writeline("{0}-{1}", item.cow, item.cowid); }
please help , motivation mind-control fluffy kitten can talk !!!
first should alter code xml string contains uploaded. way code getting string 100025 bytes of buffer.
int count = networkstream.read(bytesfrom, 0, clientsocket.receivebuffersize); string datafromclient = system.text.encoding.ascii.getstring(bytesfrom, 0, count);
now datafromclient
sent client.
the lastly step parse xmldocument class.
xmldocument doc = new xmldocument(); doc.loadxml(datafromclient); string cow = doc.selectsinglenode("monkey/chicken/cow").innertext; string cowid = doc.selectsinglenode("monkey/chicken/cowid").innertext;
update
if want utilize xdocument class here 1 way it. first select chicken elements xpath statement. next grab kid elements cow & cowid. note element names case sensitive , must match source elements. 1 of problems implementation character case in "cowid".
var monkey = xdocument.parse(x).xpathselectelements("monkey/chicken") .select(n => new { amount = n.element("cow").value, tellno = n.element("cowid").value });
c# xml sockets tcp console-application
Comments
Post a Comment