Question

I'm trying to validate XML nodes or fragments against an XML schema. I have read this article:

Validating xml nodes, not the entire document

but the chosen solution doesn't look like to work for me.

private void ValidateSubnode(XmlNode node, XmlSchema schema)
{
  XmlTextReader reader = new XmlTextReader(node.OuterXml, XmlNodeType.Element, null);

  XmlReaderSettings settings = new XmlReaderSettings();
  settings.ConformanceLevel = ConformanceLevel.Fragment;
  settings.Schemas.Add(schema);
  settings.ValidationType = ValidationType.Schema;
  settings.ValidationEventHandler += new ValidationEventHandler(XSDValidationEventHandler);

  XmlReader validationReader = XmlReader.Create(reader, settings);

  while (validationReader.Read())
  {
  }
}

private void XSDValidationEventHandler(object sender, ValidationEventArgs args)
{
  errors.AppendFormat("XSD - Severity {0} - {1}", 
                  args.Severity.ToString(), args.Message);
}

wich is, as far as I can see, the code for validate a full document, but with "ConformanceLevel.Fragment"

Thus, for example, having a schema as simple as this:

<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name="Customer">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="Address">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="Line1" type="xsd:string" />
            <xsd:element name="Line2" type="xsd:string" />
          </xsd:sequence>
       </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
</xsd:schema>

A 'root' node validates OK

<Customer>
  <Address>
    <Line1>Foo</Line1>
    <Line2>Foo2</Line2>
  </Address>
</Customer>

But any inner node doesn't validate

  <Address>
    <Line1>Foo</Line1>
    <Line2>Foo2</Line2>
  </Address>

I receive the error: "'Address' element is not declared"

Is there something I am missing?

Was it helpful?

Solution 2

Following the recomendation of MiMo, to solve the problem I modify IN RUNTIME the schema. I load it in memory and change it there. I posted my solution here:

Validate XML nodes against a schema using accesory XSD files

OTHER TIPS

Your schema does not allow stand-alone Address elements, and so when you pass only that the validation fails.

Modify your schema like this:

<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
  <xsd:element name="Address">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Line1" type="xsd:string" />
        <xsd:element name="Line2" type="xsd:string" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="Customer">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="Address"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Validation of fragments of XML with a XSD does not really work: the same element can be valid or not - or have different valid structures - depending on where it is in a document that matches the XSD, so validating a stand-alone element is not (in general) possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top