开发者

XMLDocument -> Byte[] ... how to get back to XMLDocument?

开发者 https://www.devze.com 2023-03-19 04:16 出处:网络
I have an XmlDocument and get the bytes of the object as follows: XmlDocument xmlDoc = new XmlDocument();

I have an XmlDocument and get the bytes of the object as follows:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:\\myxml.xml");

byte[] data = Encoding.UTF8.GetBytes(xmlDocument.outerXml);

and data is stored in a database.

Now I am reading the byte[] data back out, and want to get back to the XmlDocument object. How can I do this, as I cannot simply case the byte[] to an XmlDocument?

开发者_StackOverflow社区

Thanks.


You could use the LoadXml method:

byte[] data = ... fetch from your db
string xml = Encoding.UTF8.GetString(data);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
// TODO: do something with the resulting XmlDocument

UPDATE:

As requested in the comments section here's how to load the byte array into a DataTable:

byte[] data = ... fetch from your db
DataTable dt = ... fetch from somewhere or instantiate a new;
using (var stream = new MemoryStream(data))
{
    dt.ReadXml(stream);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消