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..
            Asked
            
        
        
            Active
            
        
            Viewed 8.4k times
        
    30
            
            
        - 
                    1Where do you get the byte array from? Do you know the encoding used? – R. Martinho Fernandes Apr 07 '11 at 13:51
 - 
                    XML containes base64 encoded data.. – hs2d Apr 07 '11 at 13:54
 - 
                    I meant the [character encoding](http://en.wikipedia.org/wiki/Character_encoding) of the XML document. – R. Martinho Fernandes Apr 07 '11 at 13:56
 
4 Answers
69
            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.
        R. Martinho Fernandes
        
- 228,013
 - 71
 - 433
 - 510
 
        Aliostad
        
- 80,612
 - 21
 - 160
 - 208
 
- 
                    9Good answer, although the MemoryStream is IDisposable so don't forget to wrap it in a using block! :) – firefox1986 Apr 07 '11 at 13:58
 - 
                    because of a byte order mark (BOM) I found this overload more useful (thanks Jon Skeet) `MemoryStream ms = new MemoryStream(buffer, true);` – bkwdesign Mar 14 '17 at 20:09
 
7
            
            
        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);
}
        firefox1986
        
- 1,602
 - 11
 - 9
 
4
            
            
        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));
        R. Martinho Fernandes
        
- 228,013
 - 71
 - 433
 - 510
 
        Bala R
        
- 107,317
 - 23
 - 199
 - 210
 
- 
                    Ooops... GetBytes takes a string and gives an array of bytes. GetString takes an array of bytes and gives a string. FTFY. – R. Martinho Fernandes Apr 07 '11 at 14:15
 
0
            
            
        Take a look at the System.Text.Encoding.UTF8 class. It should let you convert youre byte array into a UTF8 string.
        NKCSS
        
- 2,716
 - 1
 - 22
 - 38
 
- 
                    See [XML Encoding Defaults](http://www.opentag.com/xfaq_enc.htm) page here. UTF8 is the correct assumption is most cases. – NKCSS Apr 07 '11 at 13:53
 - 
                    1Still, I think you should have a note about it on your answer. – R. Martinho Fernandes Apr 07 '11 at 13:54