Validating XML and namespaces in .NET.

What I'm trying to do is check XML on XSD. This is all pretty simple, but I have a problem with XML without a namespace. C # only checks xml if the namespace matches the XSD namespace. This seems correct, but XML is without a namespace or other, then SchemaSet should throw an exception. Is there a property or settings to achieve this? Or do I need to get the namespace manually by reading the xmlns xml attribute?

Cleaning example:

the code:

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://example.com", @"test.xsd");
settings.Schemas.Add("http://example.com/v2", @"test2.xsd");
settings.ValidationType = ValidationType.Schema;

XmlReader r = XmlReader.Create(@"test.xml", settings);

XmlReader r = XmlReader.Create(new StringReader(xml), settings);
XmlDocument doc = new XmlDocument();
try
{
    doc.Load(r);
}
catch (XmlSchemaValidationException ex)
{

    Console.WriteLine(ex.Message);
}

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://example.com" targetNamespace="http://example.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="test">
        <xs:annotation>
            <xs:documentation>Comment describing your root element</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:pattern value="[0-9]+\.+[0-9]+" />
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
</xs:schema>

Valid XML:

<test xmlns="http://example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">112.1</test>

Invalid XML, this will not be checked:

<hello xmlns="http://example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">112.1</hello>

Error: The 'http://example.com:hello' element is not declared.

Invalid XML, but will be checked because there is no namespace:

<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">112.1</hello>

How can i fix this?

Any help is greatly appreciated.

+5
source share
2

, xml XmlSchemaValidationException, , .

, , .

. Validationflags XmlReaderSettings

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.ReportValidationWarnings;

PS: , , , . ProcessIdentityConstraints, (xs: key, xs: keyref,...) . http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschemavalidationflags.aspx.

: , , . Validator, .

private static void SchemaValidatorHandler(object sender, ValidationEventArgs e)
    {
        if (e.Severity == XmlSeverityType.Warning || e.Severity == XmlSeverityType.Error)
        {
            //Handle your exception
        }



    }

. , .

settings.ValidationEventHandler += new ValidationEventHandler(SchemaValidatorHandler);

+3

XmlDocument.Validate()
http://msdn.microsoft.com/en-us/library/ms162371.aspx

, , XmlDocument , , , . XmlValidation . http://msdn.microsoft.com/en-us/library/aa310912%28v=vs.71%29.aspx

msdn
ValidationEventHandler ValidationEventArgs.Severity, XmlSeverityType.Warning, . , . . ValidationEventHandler ValidationEventArgs.Severity, XmlSeverityType.Error, . .
,

+1

All Articles