哈希表转换为XML字符串,返回哈希表,而不使用.NET串行 [英] Convert Hashtable to xml string and back to HashTable without using .NET Serializer
本文介绍了哈希表转换为XML字符串,返回哈希表,而不使用.NET串行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道如何到一个Hashtable转换成XML字符串,然后回一个Hashtable不使用基于.NET XMLSerializer的。 XmlSerializer的提出了一些安全问题时码的IE浏览器中运行的浏览器的保护模式开启时 -
Does anyone know how to convert a Hashtable to an XML String then back to a HashTable without using the .NET based XMLSerializer. The XMLSerializer poses some security concerns when code runs inside of IE and the browser's protected mode is turned on -
所以基本上我正在寻找一种简单的方法来转换的哈希表来串并返回到一个Hashtable。
So basically I am looking for an easy way to convert that Hashtable to string and back to a Hashtable.
任何示例代码将不胜感激。
Any sample code would be greatly appreciated.
感谢
推荐答案
您可以使用的DataContractSerializer 类:
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
public class MyClass
{
public string Foo { get; set; }
public string Bar { get; set; }
}
class Program
{
static void Main()
{
var table = new Hashtable
{
{ "obj1", new MyClass { Foo = "foo", Bar = "bar" } },
{ "obj2", new MyClass { Foo = "baz" } },
};
var sb = new StringBuilder();
var serializer = new DataContractSerializer(typeof(Hashtable), new[] { typeof(MyClass) });
using (var writer = new StringWriter(sb))
using (var xmlWriter = XmlWriter.Create(writer))
{
serializer.WriteObject(xmlWriter, table);
}
Console.WriteLine(sb);
using (var reader = new StringReader(sb.ToString()))
using (var xmlReader = XmlReader.Create(reader))
{
table = (Hashtable)serializer.ReadObject(xmlReader);
}
}
}
这篇关于哈希表转换为XML字符串,返回哈希表,而不使用.NET串行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文