i have 2 methods xmlwriter , xmlreader.
have byte[] called thumbprint. in writer convert byte[] string , write xml file. works. need figure out how read in string , convert byte[].
here code far:
public void writexml(xmlwriter writer) { writer.writestartelement("agent"); writer.writestartelement("thumbprint"); var encoding = new unicodeencoding(); if (thumbprint != null) { string base64 = system.convert.tobase64string(encoding.getbytes(thumbprint.tostring())); writer.writecdata(base64); } else { writer.writeendelement(); } } public void readxml(xmlreader reader) { if (reader.isstartelement("agent")) { // // read past <agent> // reader.read(); while (true) { if (reader.isstartelement("thumbprint")) { byte[] todecodebyte = system.convert.frombase64string(thumbprint.tostring()); thumbprint = todecodebyte; } else { // // read </agent> // reader.movetocontent(); reader.readendelement(); break; } } } else { throw new xmlexception("expected <agent> element not present"); } }
xml input:
<thumbprint> <![cdata[uwb5ahmadablag0algbcahkadablafsaxqa=]]> </thumbprint>
a general solution convert byte array string when don't know encoding:
static string bytestostringconverted(byte[] bytes) { using (var stream = new memorystream(bytes)) { using (var streamreader = new streamreader(stream)) { return streamreader.readtoend(); } } }
Comments
Post a Comment