c# - How to convert byte array to xml? -
i have byte stream this:
byte[] response = { 69, 90, 69, 45, 88, 77, 76, 45, 77, 115, 103, 48, 50, 60, 77, 101, 115, 115, 97, 103, 101, 62, 13, 10, 32, 32, 60, 72, 101, 97, 100, 101, 114, 62, 13, 10, 32, 32, 32, 32, 60, 77, 101, 115, 115, 97, 103, 101, 68, 97, 116, 101, 62, 50, 48, 49, 48, 48, 51, 50, 52, 60, 47, 77, 101, 115, 115, 97, 103, 101, 68, 97, 116, 101, 62, 13, 10, 32, 32, 32, 32, 60, 77, 101, 115, 115, 97, 103, 101, 84, 105, 109, 101, 62, 49, 57, 50, 56, 48, 54, 60, 47, 77, 101, 115, 115, 97, 103, 101, 84, 105, 109, 101, 62, 13, 10, 32, 32, 60, 47, 72, 101, 97, 100, 101, 114, 62, 13, 10, 32, 32, 60, 66, 111, 100, 121, 62, 13, 10, 32, 32, 32, 32, 60, 84, 114, 97, 110, 115, 97, 99, 116, 105, 111, 110, 73, 68, 62, 51, 51, 50, 53, 50, 55, 60, 47, 84, 114, 97, 110, 115, 97, 99, 116, 105, 111, 110, 73, 68, 62, 13, 10, 32, 32, 32, 32, 60, 84, 114, 97, 110, 115, 97, 99, 116, 105, 111, 110, 78, 117, 109, 98, 101, 114, 62, 49, 50, 49, 48, 52, 55, 48, 60, 47, 84, 114, 97, 110, 115, 97, 99, 116, 105, 111, 110, 78, 117, 109, 98, 101, 114, 62, 13, 10, 32, 32, 32, 32, 60, 80, 104, 111, 110, 101, 78, 117, 109, 98, 101, 114, 62, 54, 51, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 60, 47, 80, 104, 111, 110, 101, 78, 117, 109, 98, 101, 114, 62, 13, 10, 32, 32, 32, 32, 60, 65, 109, 111, 117, 110, 116, 62, 48, 48, 48, 48, 48, 48, 50, 53, 48, 48, 60, 47, 65, 109, 111, 117, 110, 116, 62, 13, 10, 32, 32, 32, 32, 60, 82, 101, 115, 117, 108, 116, 62, 48, 51, 60, 47, 82, 101, 115, 117, 108, 116, 62, 13, 10, 32, 32, 60, 47, 66, 111, 100, 121, 62, 13, 10, 60, 47, 77, 101, 115, 115, 97, 103, 101, 62 };
i want extract xml out of it. tried following:
xmldocument doc2 = new xmldocument(); memorystream ms = new memorystream(response); doc2.load(ms);
but got exception like:
an unhandled exception of type 'system.xml.xmlexception' occurred in system.xml.dll
additional information: data @ root level invalid. line 1, position 1.
i'm new xml stuff, should doc2.load() method do? create xml file can read later, in memory collection?
the problem byte []
array represents string has non-xml characters @ beginning. if do
string s; using (var ms = new memorystream(response)) using (var reader = new streamreader(ms)) { s = reader.readtoend(); console.writeline(s); }
i see
eze-xml-msg02<message> <header> <messagedate>20100324</messagedate> <messagetime>192806</messagetime> </header> <body> <transactionid>332527</transactionid> <transactionnumber>1210470</transactionnumber> <phonenumber>639999999999</phonenumber> <amount>0000002500</amount> <result>03</result> </body> </message>
the eze-xml-msg02
has removed first. best way not store in xml in first place. if somehow cannot prevent being included in xml, do:
xmldocument doc2 = new xmldocument(); using (var ms = new memorystream(response)) using (var reader = new streamreader(ms)) { while (!reader.endofstream && reader.peek() > -1 && (char)reader.peek() != '<') reader.read(); if (!reader.endofstream) doc2.load(reader); }
Comments
Post a Comment