3

I'm using XmlReader with an attached XSD for validation.

As my XML document is being read and validated, I want to determine in my C# code the 'maxLength' value specified in the XSD for a particular element. For example, my XSD fragment is very simply defined as:

<xsd:element name="testing" minOccurs="0">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="10"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>

I can get the 'minOccurs' value easily using:

myReader.SchemaInfo.SchemaElement.MinOccurs;

But how do I get the 'maxLength' value (value of 10 in my example fragment above)???

I thought 'myReader.SchemaInfo.SchemaElement.Constraints' might give me this information, but that collection always has a 'Count' of zero.

Thanks,

Pat.

Pat McBennett
  • 33
  • 2
  • 5

3 Answers3

4

you'll find here:Accessing XML Schema Information During Document Validation a good explanation of how to do this & more.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
manji
  • 47,442
  • 5
  • 96
  • 103
  • +1 Not sure if it solves the OP's problem, but very useful information nonetheless. – Robert Harvey Oct 20 '09 at 16:55
  • Yahoo - got it working, this answer was exactly the information I needed! Although my code is complicated a bit by not being able to get this info. of the 'Text' node, and so having to skip ahead to the 'EndElement' node - but what the hell, it's working now. Thanks a million!! – Pat McBennett Oct 20 '09 at 17:38
  • 2 years later, this answer still is the best info I've managed to find on the SchemaInfo object model. Great link for anyone looking to understand the SchemaInfo object. Takes a bit of work to modify it for your needs, but it's the only way I've found so far – psubsee2003 Nov 05 '11 at 15:46
1

There are ways to do so with myReader.SchemaInfo (see najmeddine's response), but in case you need to access stuff not exposed in the SchemaInfo object...

..XSD being an XML Language. You can simply load the XSD file and using XPath find the "testing" element's definition, and its maxLength.

mjv
  • 73,152
  • 14
  • 113
  • 156
  • It would be nice if you could just punch in to the SchemaInfo, since that information appears to be already present in the reader. – Robert Harvey Oct 20 '09 at 16:48
  • Agreed, Robert. It seems najmeddine solution's pointing in this direction however the object model of SchemaInfo isn't all that well documented (at least I havn't found the right documents), and hence the idea to just "snap it off" the XSD, in the somewhat tedious but well worn way of the plain XML DOM. – mjv Oct 20 '09 at 17:00
0

I had the Problem today and the Link is no longer working.

This is the Extension Method you need

public static int GetXsdFieldMaxLength(this XmlReader reader)
{
    var schemaType = reader.SchemaInfo.SchemaType;
    if (schemaType is XmlSchemaSimpleType simpleType)
    {
        XmlSchemaSimpleTypeContent content = simpleType.Content;

        // see XmlSchemaSimpleTypeRestriction source code for list of all facet types
        if (content is XmlSchemaSimpleTypeRestriction restriction)
        {
            // XmlSchemaFacet facet
            foreach (XmlSchemaObject facet in restriction.Facets)
            {
                if (facet is XmlSchemaMaxLengthFacet ml)
                {
                    // the Value is a string, convert it to an int
                    return ml.Value.ToInt();
                }
            }
        }
    }

    return -1;
}

You can then use the function inside your XML Validation code like this:

// var settings = new XmlReaderSettings();
settings.ValidationEventHandler += (object sender, ValidationEventArgs args) =>
{
    // the actual int MaxLength is missing in the Validation Error
    var reader = sender as XmlReader;
    var maxLength = reader.GetXsdFieldMaxLength();
}
Charles
  • 2,721
  • 1
  • 9
  • 15