How do I parse element restrictions from XSD file using C# LINQ?

I am trying to parse the “workOrder.xsd” file and print out the element name, minOccurs, maxOccurs, length, and value type (ie: string, integer, etc.) to the console.  Currently the code is outputting no data.  Please see the “workOrder.xsd” and XsdParser code fragment for details.  Thank you.


<!-- workOrder.xsd -->

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
      <xs:element name="workOrder">
            <xs:annotation>
                  <xs:documentation>Comment describing your root element</xs:documentation>
            </xs:annotation>
            <xs:complexType>
                  <xs:sequence>
        <xs:element name="approve" minOccurs="1">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:length value="1"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="sourceId" minOccurs="1">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:length value="100"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="workItems" minOccurs="1" maxOccurs="unbounded">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:length value="100"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="userId" minOccurs="1" maxOccurs="1">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:length value="1000"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
                  </xs:sequence>
            </xs:complexType>
      </xs:element>
</xs:schema>


// -- Code Fragment --

private void XsdParser()
{
    XmlSchema schema;

    using (var reader = new StreamReader(@"c:\pathTo\workOrder.xsd"))
    {
        schema = XmlSchema.Read(reader, null);
    }

    XmlSchemaSet schemaSet = new XmlSchemaSet();
    schemaSet.Add(schema);
    schemaSet.Compile();

    schema = schemaSet.Schemas().Cast<XmlSchema>().First();

    var simpleTypes = schema.SchemaTypes.Values.OfType<XmlSchemaSimpleType>()
                                       .Where(t => t.Content is XmlSchemaSimpleTypeRestriction);

    foreach (var simpleType in simpleTypes)
    {
        var restriction = (XmlSchemaSimpleTypeRestriction) simpleType.Content;
        var xmlFacets = restriction.Facets.OfType<XmlSchemaFacet>();

        if (xmlFacets.Any())
        {
            Console.WriteLine("" + simpleType.Name);

            foreach (var facet in xmlFacets)
            {
                Console.WriteLine(facet.Value);
            }
        }
    }
}
ReverendProcessAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Meir RivkinFull stack Software EngineerCommented:
check this code snippet:
static void XsdParser()
        {
            XmlSchema schema;

            using (var reader = new StreamReader(@"c:\temp\1.xsd"))
            {
                schema = XmlSchema.Read(reader, null);
            }

            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.Add(schema);
            schemaSet.Compile();

            schema = schemaSet.Schemas().Cast<XmlSchema>().First();

            foreach (XmlSchemaElement element in schema.Elements.Values)
            {
                Console.WriteLine("Element: {0}", element.Name);

                // Get the complex type of the Customer element.
                XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType;

                // Get the sequence particle of the complex type.
                XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;

                // Iterate over each XmlSchemaElement in the Items collection.
                foreach (XmlSchemaElement childElement in sequence.Items)
                {
                    Console.WriteLine("Element: {0}", childElement.Name);
                    Console.WriteLine("MaxOccurs: {0}", string.IsNullOrEmpty(childElement.MaxOccursString) ? string.Empty : childElement.MaxOccursString);
                    Console.WriteLine("MinOccurs: {0}", string.IsNullOrEmpty(childElement.MinOccursString) ? string.Empty : childElement.MinOccursString);
                    
                    XmlSchemaSimpleType simpleType = childElement.ElementSchemaType as XmlSchemaSimpleType;
                    XmlSchemaSimpleTypeRestriction restriction = simpleType.Content as XmlSchemaSimpleTypeRestriction;

                    Console.WriteLine("type: {0}", restriction.BaseTypeName.Name);
                    var facets = restriction.Facets;

                    foreach (var facet in facets)
                    {
                        Console.WriteLine("length: {0}", (facet as XmlSchemaLengthFacet).Value);
                    }
                }
            }
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Meir RivkinFull stack Software EngineerCommented:
more suitable facet enumeration should be:

foreach (XmlSchemaFacet facet in facets)
                    {
                        Console.WriteLine("{0}: {1}", facet.GetType().Name, facet.Value);
                    }

replace with foreach in line 39.
ReverendProcessAuthor Commented:
The solution was spot on and easy to follow.  Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.