开发者

byte[] to XML with .NET/C#

I have XML stored in the database as a byte array (byte[]). Now I need to get that byte array from the database whic开发者_如何学Pythonh I'm successfully doing and pass it to XDocument like the following:

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

This code works fine if I pass to XDocument a file on the server in XML format. However it doesn't work if I pass a byte[] array.

Any hint how I should conert byte[] array back to XML would be greatly appreciated.

Thanks.


  using (var stream = new MemoryStream(UserData, false))
  {
     var doc = Xdocument.Load(stream);

     ...
  }

As @int3 asked, we should know the enocoding (UTF8/16, etc) that you used to store the document in the database.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜