今天重构代码时,想把如下xml文件嵌入程序集中,在运行时读取:
?xml version="1.0" encoding="utf-8"?
Convertors xmlns="http://tempuri.org/~vs24E.xsd"
Convertor
Name1/Name
Category1/Category
Description1/Description
/Convertor
Convertor
Name2/Name
Category2/Category
Description2/Description
/Convertor
Convertor
Name3/Name
Category3/Category
Description3/Description
/Convertor
/Convertors
到处找了一番,都是关于读取.txt和.resx类型的嵌入资源的,后来灵光一现,试出以下方法:
private static ConvertorData GetConvertorData()
{
Assembly assembly = typeof(ConvertorProvider).Assembly ;
System.IO.Stream stream = assembly.GetManifestResourceStream("TextConvertor.Convertor.xml") ;
ConvertorData data = new ConvertorData() ;
data.ReadXml(stream) ;
return data ;
}
大概是先得到Assembly对象,然后得到流对象,以后就好办了,要不读到XmlDocument,要不读到根据xml文件生成的数据集中。
http://www.cnblogs.com/karoc/archive/2006/11/27/574215.html