我将XML作为字节数组(byte [])存储在数据库中。现在,我需要从我成功地在做数据库的字节数组,并通过它的XDocument类似如下:byte [] to XML with .NET/C#
public Dictionary<string, string> ReadFromByte(byte[] UserData, string toplevelnode, string firstattribute, string secondattribute)
{
XDocument doc = XDocument.Load(UserData);
Dictionary<string, string> dictionary = doc.Descendants(toplevelnode).ToDictionary(x => x.Attribute(firstattribute).Value,
x => x.Attribute(secondattribute).Value);
return dictionary;
}
如果我通过到的XDocument XML格式的服务器上的文件时,此代码工作正常。但是,如果我传递一个byte []数组,它将不起作用。
任何提示我应该如何conert byte []数组回到XML将不胜感激。
谢谢。
你是如何在数据库中存储xml文档的?使用序列化还是仅使用xml内容的字节? – crypted 2011-05-23 10:54:16
您应该能够将db字段值写入MemoryStream并将该流馈送到XDocument。 – faester 2011-05-23 10:57:22
是的,你如何从数据库中获取XML取决于你如何在第一时间得到它。你需要告诉我们你是如何将它加入数据库的。 – 2011-05-23 10:58:42