今天重構代碼時,想把如下XML文件嵌入程序集中,在運行時讀取:
<?XML version="1.0" encoding="utf-8"?>
到處找了一番,都是關於讀取.txt和.resx類型的嵌入資源的,後來靈光一現,試出以下方法:
<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>
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文件生成的數據集中。