我可以在XmlSchemaSet.Add()方法的Uri参数中指定本地文件位置吗?

时间:2014-04-08 16:10:14

标签: c# .net xml schema

XmlSchemaSet.Add()方法采用Uri和目标命名空间,但是当我尝试传入本地文件位置时,会产生错误。

_schemaUri = @"L:\schemaDoc.xsd";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(_schemaUri, _targetNamespace);

ERROR:

NotSupportedException was caught

The URI prefix is not recognized.

2 个答案:

答案 0 :(得分:3)

是。您混淆了添加方法的参数。第一个参数是目标命名空间,第二个参数是URI。所以你的代码应该是这样的:

_schemaUri = @"L:\schemaDoc.xsd";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(_targetNamespace, _schemaUri);

有关详细信息,请参阅文档:

http://msdn.microsoft.com/en-us/library/1hh8b082%28v=vs.110%29.aspx

答案 1 :(得分:1)

根据MSDN文档,您具有相反顺序的schemaUri和targetNamespace参数。

来自MSDN:
XmlSchemaSet.Add Method (String, String) 在指定给XmlSchemaSet的URL处添加XML架构定义语言(XSD)架构。

Namespace:  System.Xml.Schema
Assembly:  System.Xml (in System.Xml.dll)

public XmlSchema Add(
    string targetNamespace,
    string schemaUri
)

参数

targetNamespace
Type: System.String
The schema targetNamespace property, or null to use the targetNamespace specified in the schema.

schemaUri
Type: System.String
The URL that specifies the schema to load.  

http://msdn.microsoft.com/en-us/library/1hh8b082(v=vs.110).aspx