简体   繁体   中英

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 .

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

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).

HTH

--larsw

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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