今天重构代码时,想把如下xml文件嵌入程序集中,在运行时读取:
<?xml version="1.0" encoding="utf-8"?>
<Convertors xmlns="http://tempuri.org/~vs24E.xsd">
<Convertor>
<Name>1</Name>
<Category>1</Category>
<Description>1</Description>
</Convertor>
<Convertor>
<Name>2</Name>
<Category>2</Category>
<Description>2</Description>
</Convertor>
<Convertor>
<Name>3</Name>
<Category>3</Category>
<Description>3</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
查看本文来源