validating XML node over a XSD file

别说谁变了你拦得住时间么 提交于 2020-01-05 09:11:59

问题


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?


回答1:


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.




回答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



来源:https://stackoverflow.com/questions/19232065/validating-xml-node-over-a-xsd-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!