0

i build a application for validating xml file against XSd. if error occours in one node it throws a exception, in that exception i can get only line number and line position only. how to get the maxlength value of that node.

            MemoryStream xml = new MemoryStream();
            string xsd;
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas.Add("", Application.StartupPath + "\\std_imaging.xsd");
            settings.ValidationEventHandler += MyValidationEventHandler;

            var v = XmlReader.Create(filename, settings);

            while (v.Read())
            {
                string a1 = v.ValueType.Name.Length.ToString();
                string name = v.NodeType + v.Name + v.ValueType + v.Value.ToString();
            }

       public void MyValidationEventHandler(object sender, ValidationEventArgs args)
       {
             schemaResult = false;
             textBox1.Text = textBox1.Text + (Environment.NewLine + args.Message +     
             Environment.NewLine +      "Location(" + args.Exception.LineNumber + 
             "," + args.Exception.LinePosition + ")" + Environment.NewLine);
       }

    this is my code.
jrummell
  • 42,637
  • 17
  • 112
  • 171
Prabhakaran
  • 92
  • 2
  • 10

1 Answers1

0

I ran into the same problem a while ago and learned that getting the schema info for a particular node is not as easy as one would think. I would encourage you to look at why you need this information and if it is worth the time as it is not a simple solution.

If you have a significant need for this and in the future, the best solution is to work directly with the IXmlSchemaInfo object model so you can read any of the schema info you want. Take a look at the following question and this blog post in the accepted answer:

In C#, how to determine the XSD-defined MaxLength for an element

It shows you a great deal about the IXmlSchemaInfo object model. It basically involves a lot of type checking and casting. Since you are using XmlReader, the SchemaInfo should be part of the reader, so you can grab the schema information for the current node.

The blog shows you how to read the schema info for any node and actually does not tie it into the validation as all, but you can easily adapt it to work within your Validation callback method.

public void MyValidationEventHandler(object sender, ValidationEventArgs args)
{
    // Using `getMaxLength` method from blog code will return a list of max 
    //  lengths.  If there is more than one, you need to decide how to handle it.
    //  It would also be a good idea to add some addition null checking
    List<int> maxLengths = getMaxLength(sender as XmlReader);

    schemaResult = false;
    textBox1.Text = textBox1.Text + (Environment.NewLine + args.Message +     
        Environment.NewLine + "Location(" + args.Exception.LineNumber + 
        "," + args.Exception.LinePosition + ")" + Environment.NewLine);

}
Community
  • 1
  • 1
psubsee2003
  • 8,563
  • 8
  • 61
  • 79