如何获取嵌入资源的资源 Uri?
我有一个方法如下,它根据 xsd 文件验证 xml。此方法在 MSTest 中使用:
public static string Validate(string xml, string relativeXsdFilePath)
{
var schemas = new XmlSchemaSet();
// relativeXsdFilePath like 'TestData.Xsd.XsdFile1.xsd'
var schemaUri = ResourceHelper.GetPath(relativeXsdFilePath);
schemas.Add("", schemaUri);
XDocument doc = XDocument.Load(new StringReader(xml));
string msg = null;
doc.Validate(schemas, (o, e) =>
{
msg = e.Message;
});
return msg;
}
到目前为止,我正在实施以下方法,但没有成功!:
public static string GetPath(string relativeFileName)
{
var assemblyName = new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name;
var resourceName = GetResourceName(relativeFileName);
// resource name would be like 'ProjectName.UnitTests.TestData.Xsd.profile.xsd'
var result = string.Format("/{0};component/{1}", assemblyName, resourceName);
return result;
}
我的 XSD 位于此处:
ProjectRoot
---TestData
------Xsd
---------XsdFile1.xsd