简体   繁体   中英

c# create xml from byte array

i have xml what i get as byte array, whats the best way to get the xml string out of it? I was tryng to use xmltextreader and memorystream but with no success..

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));

Take a look at the System.Text.Encoding.UTF8 class. It should let you convert youre byte array into a UTF8 string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM