大家好我有我的XML
文件如下
的名字 XML
XMLFile2.xml
<?xml version="1.0"?>
<Product ProductID="123"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Product.xsd">
<ProductName>XYZ</ProductName>
</Product>
Run Code Online (Sandbox Code Playgroud)
我的XSD
情况如下
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Product"
targetNamespace="http://tempuri.org/Product.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/Product.xsd"
xmlns:mstns="http://tempuri.org/Product.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Product">
<xs:complexType>
<xs:sequence>
<xs:element name="ProductName" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="ProductID" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
这是我的代码
string strPath = Server.MapPath("XMLFile2.xml");
XmlTextReader r = new XmlTextReader(strPath);
XmlValidatingReader v = new XmlValidatingReader(r);
v.ValidationType = ValidationType.Schema;
v.ValidationEventHandler +=
new ValidationEventHandler(MyValidationEventHandler);
while (v.Read())
{
}
v.Close();
if (isValid)
Response.Write("Document is valid");
else
Response.Write("Document is invalid");
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Validation event
The targetNamespace parameter '' should be the same value as the targetNamespace 'http://tempuri.org/Product.xsd' of the schema.Validation event
The 'Product' element is not declared.Validation event
Could not find schema information for the attribute 'ProductID'.Validation event
The 'ProductName' element is not declared.Document is invalid
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我哪里出错了.
您的XSD设置为验证"http://tempuri.org/Product.xsd"
命名空间,但您的XML仅包含""
命名空间中的元素.
您需要(a)更改XML文件以使用"http://tempuri.org/Product.xsd"
命名空间,或(b)更改XSD文件以使用""
命名空间,具体取决于您的用户要求.