简体   繁体   English

即时反序列化,或 LINQ 到 XML

[英]Deserialize on the fly, or LINQ to XML

Working with C# Visual Studio 2008, MVC1.使用 C# Visual Studio 2008,MVC1。

I'm creating an xml file by fetching one from a WebService and adding some nodes to it.我通过从 WebService 中获取一个文件并向其添加一些节点来创建一个 xml 文件。 Now I wanted to deserialize it to a class which is the model used to strongtyped the View.现在我想将它反序列化为 class,这是用于对视图进行强类型化的 model。

First of all, I'm facing problems to achieve that without storing the xml in the filesystem cause I don't know how this serialize and deserialize work.首先,我在不将 xml 存储在文件系统中的情况下实现这一点时遇到了问题,因为我不知道这个序列化和反序列化是如何工作的。 I guess there's a way and it's a matter of time.我想是有办法的,只是时间问题。

But, searching for the previous in the web I came accross LINQ to XML and now I doubt whether is better to use it.但是,在 web 中搜索前一个,我遇到了 LINQ 到 XML,现在我怀疑是否最好使用它。

The xml would be formed by some clients details, and basically I will use all of them. xml 会由一些客户详细信息组成,基本上我会使用所有这些信息。

Any hint?有什么提示吗?

Thanks!!谢谢!!

try this:试试这个:

XmlSerializer xmls = new XmlSerializer(typeof(XElement)); 
FileStream FStream;
try 
{             
   FStream = new FileStream(doctorsPath, FileMode.Open);
  _Doctors = (XElement)xmls.Deserialize(FStream); FStream.Close(); 
   FStream = new FileStream(patientsPath, FileMode.Open); 
  _Patients = (XElement)xmls.Deserialize(FStream)
   FStream.Close(); 
   FStream = new FileStream(treatmentsPath, FileMode.Open);
  _Treatments = (XElement)xmls.Deserialize(FStream);
    FStream.Close(); 
} 
catch 

{ }

This will load all of the XML files into our XElement variables.这会将所有 XML 文件加载到我们的 XElement 变量中。 The try – catch block is a form of exception handling that ensures that if one of the functions in the try block throws an exception, the program will jump to the catch section where nothing will happen. try-catch 块是一种异常处理形式,它确保如果 try 块中的某个函数抛出异常,程序将跳转到 catch 部分,那里什么也不会发生。 When working with files, especially reading files, it is a good idea to work with try – catch.在处理文件时,尤其是读取文件时,最好使用 try – catch。

You can save a XElement to and from a MemoryStream (no need to save it to a file stream)您可以将 XElement 保存到MemoryStream或从中保存(无需将其保存到文件流)

 MemoryStream ms = new MemoryStream();
 XmlWriter xw = XmlWriter.Create(ms);
 document.Save(xw);
 xw.Flush();

Then if you reset the position back to 0 you can deserialize it using the DataContractSerializer.然后,如果您将 position 重置回 0,您可以使用 DataContractSerializer 将其反序列化。

 ms.Position = 0;
 DataContractSerializer serializer = new DataContractSerializer(typeof(Model));
 Model model = (model) serializer.ReadObject(ms);

There are other options for how serialization works, so if this is not what you have, let me know what you are using and I will help.对于序列化的工作方式还有其他选择,所以如果这不是您所拥有的,请告诉我您正在使用什么,我会提供帮助。

LINQ to XML is an excellent feature. LINQ 到 XML 是一个很好的功能。 You can always rely on that.你总是可以依靠它。 You don't need to write or read or data from file.您不需要从文件中写入或读取数据。 You can specify either string or stream to the XDocument您可以为 XDocument 指定字符串或 stream

There are enough ways to load an XML element to the XDocument object. See the appropriate Load functions .有足够多的方法可以将 XML 元素加载到 XDocument object。 请参阅相应的加载函数 Once you load the content, you can easily add/remove the elements and later you can save to disk if you want.加载内容后,您可以轻松添加/删除元素,稍后您可以根据需要保存到磁盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM