简体   繁体   中英

Can't find “schemaLocation” attribute

I'm creating a schema to validate some XML, but when it comes down to actually reading in the document, I'm getting the error:

The 'http://www.w3.org/2001/XMLSchema:schemaLocation' attribute is not declared.

This is what the beginning of one of the XML files using the schema looks like.

<?xml version="1.0"?>
<envelope xsi:schemaLocation="C:\LocalPath MySchema.xsd" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema" 
 xmlns="http://tempuri.org/MySchema.xsd">
...
</envelope>

My validation code looks like this:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
Settings.Schemas.Add(@"http://tempuri.org/MySchema.xsd",
@"C:\LocalPath\ MySchema.xsd");
XmlReader reader = XmlReader.Create(@"C:\LocalPath\testxml\somefile.xml", settings);
xmlDoc.Load(reader);

ValidationEventHandler eventHander = new ValidationEventHandler(validationHandler);

xmlDoc.Validate(eventHander);

The namespace http://www.w3.org/2001/XMLSchema (with conventional prefix xsd or xs ) is for schema documents; the schemaLocation attribute you want is in the namespace http://www.w3.org/2001/XMLSchema-instance (which has the conventional prefix xsi for "XML Schema Instance namespace").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM