簡體   English   中英

.NET / C#將byte []轉換為XML

[英]byte[] to XML with .NET/C#

我將XML作為字節數組(byte [])存儲在數據庫中。 現在,我需要從數據庫中獲取成功完成的字節數組,並將其傳遞給XDocument,如下所示:

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

如果我將XML格式的文件傳遞給XDocument,則此代碼可以正常工作。 但是,如果我傳遞一個byte []數組,它將不起作用。

我將如何將byte []數組轉換回XML的任何提示將不勝感激。

謝謝。

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

     ...
  }

按照@ int3的要求,我們應該知道您用來將文檔存儲在數據庫中的編碼(UTF8 / 16等)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM