Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6542935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:17:09+00:00 2026-05-25T11:17:09+00:00

I have an XML file and I’m inferring its XSD schema in run-time, using

  • 0

I have an XML file and I’m inferring its XSD schema in run-time, using the XmlSchemaInference class.

Sample file:

<products>
    <product id="1" name="t-shirt">
        <size name="medium"/>
        <size name="large"/>
        <price>
            <net>10</net>
            <gross>25</gross>
        </price>
    </product>
    <product id="2" name="computer mouse">  
        <price>
            <net>50</net>       
        </price>
    </product>
</products>

It does work – it infers the schema nicely:

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="products">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="product">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="0" maxOccurs="unbounded" name="size">
                <xs:complexType>
                  <xs:attribute name="name" type="xs:string" use="required" />
                </xs:complexType>
              </xs:element>
              <xs:element name="price">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="net" type="xs:unsignedByte" />
                    <xs:element minOccurs="0" name="gross" type="xs:unsignedByte" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="id" type="xs:unsignedByte" use="required" />
            <xs:attribute name="name" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

The question is:

How can I iterate (recursively?) through all the elements from this schema? How are they stored by the XmlSchemaSet class? I need to present them to the user so they can do some mapping.

I am retrieving an XmlSchema from XmlSchemaSet.Schemas property, and then what? XmlSchema.Elements only contains one item (products), and I can’t find any way to look up what its subelements are.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:17:10+00:00Added an answer on May 25, 2026 at 11:17 am

    Okay! No answer and not much interest – I figured it out on my own.

    I used code from this MSDN article I googled up: Traversing XML Schemas

    And I based my recursive solution on it.

    void PrintSchema(string xmlFilePath)
    {
        var schemaSet = new XmlSchemaInference().InferSchema(XmlReader.Create(xmlFilePath));
        foreach (XmlSchemaElement element in schemaSet
            .Schemas()
            .Cast<XmlSchema>()
            .SelectMany(s => s.Elements.Values.Cast<XmlSchemaElement>()))
        {
            Debug.WriteLine(element.Name + " (element)");
            IterateOverElement(element.Name, element);
        }
    }
    
    void IterateOverElement(string root, XmlSchemaElement element)
    {
        var complexType = element.ElementSchemaType as XmlSchemaComplexType;
        if (complexType == null) 
        {
            return;
        }
        if (complexType.AttributeUses.Count > 0)
        {
            var enumerator = complexType.AttributeUses.GetEnumerator();
            while (enumerator.MoveNext())
            {
                var attribute = (XmlSchemaAttribute)enumerator.Value;
                Debug.WriteLine(root + "." + attribute.Name + " (attribute)");
            }
        }
        var sequence = complexType.ContentTypeParticle as XmlSchemaSequence;
        if (sequence == null) 
        {
            return;
        }
        foreach (XmlSchemaElement childElement in sequence.Items)
        {
            root += String.Concat(".", childElement.Name);
            Debug.WriteLine(root + " (element)");
            // recursion
            IterateOverElement(root, childElement);
        }
    }
    

    The output is:

    products (element)
    products.product (element)
    products.product.id (attribute)
    products.product.name (attribute)
    products.product.size (element)
    products.product.size.name (attribute)
    products.product.price (element)
    products.product.price.net (element)
    products.product.price.gross (element)
    

    I leave to you to judge how friendly this API is, especially given how scarce is the MSDN documentation on these particular classes. Any comments or insights are appreciated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file and an XML schema in another file and I'd
I have xml file in server. I am parsing this xml using DOM(xml is
I have xml file whose structure is defined with following xsd: <?xml version=1.0 encoding=utf-8?>
I have xml file like following for displaying ads using asp:AdRotator in asp <?xml
I have an xml file I wish to transform using an xsl-document, but I
I have xml File with element date type: ... <startDate /> ... in xsd
I have xml file with TAG like this: <Question>dzia&amp;#322;owa</Question> I'm reading this file using
What is the easiest way to convert xml to html? I have xml file
I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have an xml file providing data for a datagrid in Flex 2 that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.