You should already be able to read simple values from the database - in this case it will be easy to get XML from there.
First you need to get an array of bytes:
var bytes = myReader["имя_поля"] as byte[]; if (bytes == null) { /* что-нибудь сделать */ }
Now you need to build from this set of bytes XML. To do this, you can wrap them in a MemoryStream and read the XML from the stream:
var doc = new XmlDocument(); doc.Load(new MemoryStream(bytes));
If you prefer to use XLinq, then the MemoryStream also great:
var doc = XDocument.Load(new MemoryStream(bytes));
Separately, I note that the methods with pre-conversion to the string are incorrect - because you cannot know in advance what XML encoding is written in (the XML encoding is specified within the XML declaration according to the standard). In addition, when using UTF-8 and pre-converting to a string, there may be a problem with the BOM at the beginning of the file.
If for some reason the XML documents are in the database without encoding - you can wrap the MemoryStream in a StreamReader :
var doc = new XmlDocument(); doc.Load(new StreamReader(new MemoryStream(bytes), Encoding.ВашаКодировка)); var doc = XDocument.Load(new StreamReader(new MemoryStream(bytes), Encoding.ВашаКодировка));
But in such cases, I recommend changing the column type from varbinary to nvarchar ! It will be even easier to work with him:
var xmlstr = myReader["имя_поля"] as string; if (xmlstr == null) { /* что-нибудь сделать */ } var doc = new XmlDocument(); doc.LoadXml(xmlstr); var doc = XDocument.Parse(xmlstr);