Having XML schema with the namespace http://mynamespace. If the wrong XML document with default namespace xmlns="http://mynamespace" is validated then exception is thrown as expected. While if someone change the namespace(http://Wrongnamespace) this wrong XML will pass validation.
Here is the unit test with schema validation. Method XSD_NotValid_2 is not working properly:
[TestClass]
public class XSDTest
{
public System.Xml.XmlReaderSettings ReaderSettings
{
get
{
string sXSD = "<xsd:schema targetNamespace=\"http://mynamespace\" xmlns=\"http://mynamespace\""
+ " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\">"
+ "<xsd:element name=\"Root\">"
+ "<xsd:complexType>"
+ "<xsd:sequence>"
+ "<xsd:element name=\"Child\" minOccurs=\"1\" maxOccurs=\"1\" />"
+ "</xsd:sequence>"
+ "</xsd:complexType>"
+ "</xsd:element>"
+ "</xsd:schema>";
System.Xml.Schema.XmlSchema schema = System.Xml.Schema.XmlSchema.Read(new System.IO.StringReader(sXSD)
, new System.Xml.Schema.ValidationEventHandler(OnValidationFail));
System.Xml.XmlReaderSettings readerSettings_Ret = new System.Xml.XmlReaderSettings();
readerSettings_Ret.ValidationType = System.Xml.ValidationType.Schema;
readerSettings_Ret.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(OnValidationFail);
readerSettings_Ret.Schemas.Add(schema);
return readerSettings_Ret;
}
}
private void OnValidationFail(object s, System.Xml.Schema.ValidationEventArgs e)
{
throw new OperationCanceledException("Validation error: " + e.Message);
}
[TestMethod]
public void XSD_Valid_Test()
{
// Valid elements and valid namespace
String sXML_Valid = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<Root xmlns=\"http://mynamespace\"><Child /></Root>";
System.Xml.XmlReader xmlReader_Valid =
System.Xml.XmlReader.Create(new System.IO.StringReader(sXML_Valid), this.ReaderSettings);
while (xmlReader_Valid.Read()) { } // no fail expected
}
[TestMethod]
[ExpectedException(typeof(OperationCanceledException))]
public void XSD_NotValid_1()
{
// No valid elements, while valid namespace
String sXML_NotValid_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<BadRoot xmlns=\"http://mynamespace\"><Child /></BadRoot>";
System.Xml.XmlReader xmlReader_NoValid_1 =
System.Xml.XmlReader.Create(new System.IO.StringReader(sXML_NotValid_1), this.ReaderSettings);
while (xmlReader_NoValid_1.Read()) ;
}
[TestMethod]
[ExpectedException(typeof(OperationCanceledException))]
public void XSD_NotValid_2()
{
// No valid elements and no valid namespace
String sXML_NotValid_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<Root xmlns=\"http://Wrongnamespace\"><NotValidChild /></Root>";
System.Xml.XmlReader xmlReader_NoValid_2 =
System.Xml.XmlReader.Create(new System.IO.StringReader(sXML_NotValid_2), this.ReaderSettings);
while (xmlReader_NoValid_2.Read()) ;
}
}
Is it normal behavior? How to force correct namespace targeting?
And also how to force Root element to be required in the case XSD has additional Root2 element?
If you enable schema validation warnings, you’ll get the following error:
Use
A couple of small things:
usingblocks. In fact, I’d say especially in a unit test, where each test should be independent of the other tests, you want to make sure that one test has cleaned up before starting the next.