什么是写和下载xml文件在asp.net的最佳途径? [英] What's the best way to write and download xml file in asp.net?

查看:90
本文介绍了什么是写和下载xml文件在asp.net的最佳途径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页形式,即需要保存用户输入,到一个文件,并在客户端PC中的数据,也将能够从所保存的文件的读取和在稍后的时间重新填充的字段。没有文件将被保存到服务器端,所以我希望流需要在写作的时间参与。

I have a web form, that needs to save the data that the user enters, into a file and on the client pc, that will also be able to read from the saved file and repopulate the fields at a later time. No files will be saved to the server side, so I expect streaming needs to be involved at writing time.

我决定XML将是一个简单的方法来做到这一点,但我阻碍方法论。 XML文件? XML作家?我难倒了正确的搜索字词,甚至得到我想要的东西。

I decided XML would be an easy way to do this, but I'm stymied on methodology. XML documents? XML Writers? I'm stumped on the right search terms to even get what I want.

先谢谢您的任何指针。

推荐答案

您需要使用XML序列化。看看这个 MSDN文章 。下面是关于序列化和反序列化的摘录:

You'll want to use XML serialization. Take a look at this MSDN article. Here's an excerpt on serialization and deserialization:

如何将对象序列

序列化和对象,我们需要几
  中构建的类的实例。所以
  让我们先创建一个实例
  从的System.Xml XmlDocument类
  命名空间。然后创建的实例
  从XmlSerializer类
  System.Xml.Serialization命名空间
  以参数作为对象类型。现在
  刚刚创建的一个实例
  从System.IO类的MemoryStream
  命名空间是要帮助我们
  持有序列化的数据。因此,所有的
  实例都在那里,现在你需要
  调用它们的方法,让你的
  serialzed对象的XML格式。我的
  函数序列化对象的外观
  像下面。

To Serialize and object, we need few instances of the in-built classes. So lets first create an instance of a XmlDocument class from System.Xml namespace. Then create an instance of XmlSerializer class from System.Xml.Serialization namespace with parameter as the object type. Now just create an instance of the MemoryStream class from System.IO namespace that is going to help us to hold the serialized data. So all your instances are there, now you need to call their methods and get your serialzed object in the xml format. My function to Serialize an object looks like following.

私人字符串SerializeAnObject(obj对象)

private string SerializeAnObject(object obj)

{

System.Xml.XmlDocument doc = new XmlDocument();

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

System.IO.MemoryStream stream = new System.IO.MemoryStream();

try

{

    serializer.Serialize(stream, obj);

    stream.Position = 0;

    doc.Load(stream);

    return doc.InnerXml;

}

catch

{

    throw;

}

finally

{

    stream.Close();

    stream.Dispose();

}


  
  

}

}

如何反序列化对象

要反序列化一个对象,你需要一个
  StringReader,的XmlReader实例
  和XmlSerializer类,以
  读取XML数据(序列化数据)
  它读入的XmlReader和反序列化
  它分别。因此,在简短的我
  功能反序列化对象
  看起来像下面。

To DeSerialize an object you need an instance of StringReader, XmlReader and XmlSerializer class in order to read the xml data (Serialized data), read it into XmlReader and DeSerialize it respectively. So in brief my function to DeSerialize the object looks like following.

私有对象DeSerializeAnObject(字符串xmlOfAnObject)

private object DeSerializeAnObject(string xmlOfAnObject)

{

MyClass myObject = new MyClass();

System.IO.StringReader read = new StringReader(xmlOfAnObject);

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(myObject.GetType());

System.Xml.XmlReader reader = new XmlTextReader(read);

try

{

    myObject = (MyClass)serializer.Deserialize(reader);

    return myObject;

}

catch

{

throw;

}

finally

{

    reader.Close();

    read.Close();

    read.Dispose();

}


  
  

}

}

这篇关于什么是写和下载xml文件在asp.net的最佳途径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆