here i made a small app: console application, just copy all in a class
and run the console app:
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
namespace ConsoleAppXSDTestApp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
string sXSD = "<xs:schema
xmlns:xs=\"
http://www.w3.org/2001/XMLSchema\"
targetNamespace=\"
http://www.testuri.com/test\"
elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\"
[quoted text, click to view] >"
+ " <xs:element name=\"Test\">"
+ " <xs:annotation>"
+ " <xs:documentation>Comment describing your root
element</xs:documentation>"
+ " </xs:annotation>"
+ " <xs:complexType>"
+ " <xs:sequence>"
+ " <xs:element name=\"Person\">"
+ " <xs:complexType>"
+ " <xs:sequence>"
+ " <xs:element name=\"Name\">"
+ " <xs:simpleType>"
+ " <xs:restriction base=\"xs:string\">"
+ " <xs:minLength value=\"4\"/>"
+ " <xs:maxLength value=\"4\"/>"
+ " </xs:restriction>"
+ " </xs:simpleType>"
+ " </xs:element>"
+ " <xs:element name=\"Address\">"
+ " <xs:simpleType>"
+ " <xs:restriction base=\"xs:string\">"
+ " <xs:minLength value=\"7\"/>"
+ " <xs:maxLength value=\"7\"/>"
+ " </xs:restriction>"
+ " </xs:simpleType>"
+ " </xs:element>"
+ " </xs:sequence>"
+ " </xs:complexType>"
+ " </xs:element>"
+ " </xs:sequence>"
+ " </xs:complexType>"
+ " </xs:element>"
+ " </xs:schema>";
string sXmlWithDefault = "<Test
xmlns=\"
http://www.testuri.com/test\">"
+ " <Person >"
+ " <Name>namee</Name>" //here the xsd file should give error,
only 4 characthers allowed by xsd
+ " <Address>address</Address>"
+ "</Person>"
+ "</Test>";
string sXmlWithoutDefault = "<Test>"
+ " <Person >"
+ " <Name>namee</Name>" //here the xsd file should give error,
only 4 characthers allowed by xsd
+ " <Address>address</Address>"
+ "</Person>"
+ "</Test>";
Console.WriteLine("Check XML - XSD");
// //TEST 1
// try
// {
// Console.WriteLine("Check with xml file with default
namespace...");
// XmlTextReader xtrDocu = new XmlTextReader(new
StringReader(sXmlWithDefault));
// XmlTextReader xtrSchema = new XmlTextReader(new
StringReader(sXSD));
// System.Xml.Schema.XmlSchema schema = XmlSchema.Read(xtrSchema,
null);
// XmlValidatingReader reader = new XmlValidatingReader(xtrDocu);
// reader.ValidationEventHandler += new
ValidationEventHandler(reader_ValidationEventHandler);
// reader.Schemas.Add(schema);
// while(reader.Read()){};
// Console.WriteLine("success --> NOT EXPECTED");
// }
// catch(System.Exception)
// {
// Console.WriteLine("Crash As Expected");
// }
//END TEST 1
// //TEST 2
// try
// {
// Console.WriteLine("Check with xml file without default
namespace...");
// XmlTextReader xtrDocu = new XmlTextReader(new
StringReader(sXmlWithoutDefault));
// XmlTextReader xtrSchema = new XmlTextReader(new
StringReader(sXSD));
// System.Xml.Schema.XmlSchema schema = XmlSchema.Read(xtrSchema,
null);
// XmlValidatingReader reader = new XmlValidatingReader(xtrDocu);
// reader.ValidationEventHandler += new
ValidationEventHandler(reader_ValidationEventHandler);
// reader.Schemas.Add(schema);
// while(reader.Read()){};
// Console.WriteLine("success --> NOT EXPECTED");
// }
// catch(System.Exception)
// {
// Console.WriteLine("Crash As Expected");
// }
// //END TEST 2
// //TEST 3
// try
// {
// Console.WriteLine("Check with xml file without default
namespace...");
// XmlTextReader xtrDocu = new XmlTextReader(new
StringReader(sXmlWithoutDefault));
// XmlTextReader xtrSchema = new XmlTextReader(new
StringReader(sXSD));
// System.Xml.Schema.XmlSchema schema = XmlSchema.Read(xtrSchema,
null);
//
// XmlNamespaceManager nspmgr = new
System.Xml.XmlNamespaceManager(xtrDocu.NameTable);
// nspmgr.AddNamespace(string.Empty,"
http://www.testuri.com/test");
//default namespace added
// XmlParserContext context = new
XmlParserContext(xtrDocu.NameTable,nspmgr,xtrDocu.XmlLang,xtrDocu.XmlSpace);
//
// XmlValidatingReader reader = new XmlValidatingReader(new
StringReader(sXmlWithoutDefault).ReadToEnd(),XmlNodeType.Document,context);
// reader.ValidationEventHandler += new
ValidationEventHandler(reader_ValidationEventHandler);
// reader.Schemas.Add(schema);
// while(reader.Read()){};
// Console.WriteLine("success --> NOT EXPECTED");
// }
// catch(System.Exception)
// {
// Console.WriteLine("Crash As Expected");
// }
// //END TEST 3
//TEST 4
try
{
Console.WriteLine("Check with xml file without default
namespace...");
XmlTextReader xtrDocu = new XmlTextReader(new
StringReader(sXmlWithoutDefault));
XmlTextReader xtrSchema = new XmlTextReader(new
StringReader(sXSD));
System.Xml.Schema.XmlSchema schema = XmlSchema.Read(xtrSchema,
null);
schema.TargetNamespace = null;
XmlValidatingReader reader = new XmlValidatingReader(xtrDocu);
reader.ValidationEventHandler += new
ValidationEventHandler(reader_ValidationEventHandler);
reader.Schemas.Add(schema);
while(reader.Read()){};
Console.WriteLine("success --> NOT EXPECTED");
}
catch(System.Exception)
{
Console.WriteLine("Crash As Expected");
}
//END TEST 4
}
catch(System.Exception ex)
{
Console.WriteLine("Ex: " + ex.Message);
}
finally
{
Console.ReadLine();
}
}
static void reader_ValidationEventHandler(object sender,
System.Xml.Schema.ValidationEventArgs eventArgs)
{
Console.WriteLine("Error: " + eventArgs.Message);
Console.ReadLine();
}
}
}
//end of test app ;)
to run the test, put all the test in comment except the one you would
like to run ;) (not the best app, but had to be done quick)
now, you can see there are 3 test cases.
TEST 1 : gives error as expected.
TEST 2: does not give error : offcourse default namespace isn't added
in the file -> validater does not find xsd (i think that's the reason)
TEST 3: does not give error , i don't understand, because i add default
namespace -> it's the same namespace as the target namespace in XSD
TEST 4: gives error, no default namespace in file, but i remove
targetnamespace in XSD (i do not understand why this works, but TEST 3
not.)