日期:2014-05-17 浏览次数:20726 次
public static string ObjectToXml(object obj, bool toBeIndented, Type type)
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
UTF8Encoding encoding = new UTF8Encoding(false);
XmlSerializer serializer = new XmlSerializer(type);
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, encoding);
writer.Formatting = (toBeIndented ? Formatting.Indented : Formatting.None);
serializer.Serialize(writer, obj);
string xml = encoding.GetString(stream.ToArray());
writer.Close();
return xml;
}
public static object XmlToObject(string xml, Type type)
{
if (xml == null)
{
throw new ArgumentNullException("xml");
}
object o = null;
XmlSerializer serializer = new XmlSerializer(type);
StringReader strReader = new StringReader(xml);
XmlReader reader = new XmlTextReader(strReader);
try