今天重构代码时,想把如下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文件生成的数据集中。
新闻热点
疑难解答