简体   繁体   English

如何在C#中嵌入XML文件并反序列化?

[英]How to embed an XML file and deserialize from it in C#?

在C#中,有什么方法可以嵌入XML文件,在该文件中我在项目中序列化了一些类,然后将其打开并从中反序列化我的类?

By embedding - do you mean embedding in the assembly? 嵌入-您是说要嵌入到装配体中? In visual studio, add the xml file to your project and set the Build Action in the Properties window to Embedded Resource . 在Visual Studio中,将xml文件添加到您的项目中,然后在“ 属性”窗口中将“ 构建操作”设置为Embedded Resource

Next, in your C# code, you can get a hold of the resource stream (described here - works the same way for all kinds of embedded content): http://msdn.microsoft.com/en-us/library/aa287676(VS.71).aspx 接下来,在您的C#代码中,您可以获取资源流(此处描述-对所有嵌入式内容的工作方式相同): http : //msdn.microsoft.com/zh-cn/library/aa287676( VS.71)的.aspx

When you have the stream, you can first initialize an XmlSerializer instance with the System.Type you want to deserialize, and then call .Deserialize(stream) (and cast it to the right type again). 当拥有流时,可以首先使用要反序列化的System.Type初始化XmlSerializer实例,然后调用.Deserialize(stream)(然后将其再次转换为正确的类型)。

HTH HTH

--larsw --larsw

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

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