c# create xml from byte array c# create xml from byte array arrays arrays

c# create xml from byte array


XmlDocument doc = new XmlDocument();string xml = Encoding.UTF8.GetString(buffer);doc.LoadXml(xml);

OR

XmlDocument doc = new XmlDocument();MemoryStream ms = new MemoryStream(buffer);doc.Load(ms);

This assumes your data has UTF8 encoding which is the usual for XML. Also buffer here is the byte array.


Assuming your xml is in the default 'UTF8' encoding., you could do something like this;

string xml = System.Text.UTF8Encoding.UTF8.GetString(bytes);System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument().LoadXml(xml);

Or this;

XmlDocument doc = new XmlDocument();using (MemoryStream ms = new MemoryStream(buffer)){    doc.Load(ms);}


Based on the Encoding, you can do

string xmlString = System.Text.UTF8Encoding.UTF8.GetString(bytes);

and use the string

XmlTextReader reader = new XmlTextReader(new StringReader(xmlString));