locked
how to ignore to validate attributes and node in xml when validating with xsd RRS feed

  • Question

  • User910199987 posted

    hi 

    Hi, I know how to validate an xml through a  XSD when xsd residing on file system but my problem is that how to ignore to validate attributes and node in xml when validating with xsd. i want only validate attributes's datatype.

    thx

    Thursday, September 3, 2015 10:39 AM

Answers

  • User614698185 posted

    Hi rakesh_42,

    Based on your requirement, I think it could not validate attributes' datatype in xml directly. You could validate the parts of XML document. I created the sample method, code shown below:

    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);
    
        using (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);
    }

    For more information, please refer to the document about XmlSchemaValidator Class.

    I hope this will help you.
    Best Regards,
    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 4, 2015 4:01 AM

All replies

  • User614698185 posted

    Hi rakesh_42,

    Based on your requirement, I think it could not validate attributes' datatype in xml directly. You could validate the parts of XML document. I created the sample method, code shown below:

    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);
    
        using (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);
    }

    For more information, please refer to the document about XmlSchemaValidator Class.

    I hope this will help you.
    Best Regards,
    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 4, 2015 4:01 AM
  • User910199987 posted

    thanks now working perfectly.....

    Monday, September 7, 2015 5:11 AM