Get Intrinsic Simple Types Names from System.Xml.Schema.DatatypeImplementation : Schema « XML « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » XML » SchemaScreenshots 
Get Intrinsic Simple Types Names from System.Xml.Schema.DatatypeImplementation
  


using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;

namespace Thinktecture.Tools.Wscf.Services.ServiceDescription.Helpers
{
    internal static class SchemaUtility
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        internal static ArrayList GetIntrinsicSimpleTypesNames()
        {
            ArrayList primitiveNames = new ArrayList();
            Assembly assembly = Assembly.GetAssembly(typeof(XmlSchema));

            Type type = assembly.GetType("System.Xml.Schema.DatatypeImplementation");
            FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic);

            foreach (FieldInfo fi in fields)
            {
                int index = fi.Name.IndexOf("c_");
                if (index > -1)
                {
                    string fieldName = fi.Name.Substring(index + 2);
                    primitiveNames.Add("xsd:" + fieldName);
                }
            }

            return primitiveNames;
        }

        /// <summary>
        /// Reads a XML schema file and returns the information found in that.
        /// </summary>
        /// <param name="schemaFile">The XML schema file to read information from.</param>
        /// <param name="schemaNamespace">Ouput parameter which returns the namespace of the specified XML schema file.</param>
        /// <returns>
        /// An <see cref="ArrayList"/> with three items. 
        /// 1. Contains an <see cref="ArrayList"/> of <see cref="XmlSchemaElement"/> objects.
        /// 2. Contains an <see cref="ArrayList"/> of schema element names.
        /// 3. Contains a <see cref="SchemaElements"/> object. 
        /// </returns>
        internal static ArrayList GetSchemasFromXsd(string schemaFile, out string schemaNamespace)
        {
            XmlTextReader reader = null;
            ArrayList schemas;
            ArrayList schemaNames;
      List<SchemaElement> sElements;

            try
            {
                reader = new XmlTextReader(schemaFile);

                XmlSchema schema = XmlSchema.Read(reader, null);
                string schemaTargetNamesapce = schema.TargetNamespace;
                schemaNamespace = schemaTargetNamesapce;

                ArrayList xmlSchemaElements = new ArrayList();
                schemas = new ArrayList();
                schemaNames = new ArrayList();
        sElements = new List<SchemaElement>();

                foreach (XmlSchemaObject xmlObj in schema.Items)
                {
                    if (xmlObj is XmlSchemaAnnotatedxmlSchemaElements.Add(xmlObj);
                }

                foreach (XmlSchemaAnnotated obj in xmlSchemaElements)
                {
                    if (obj is XmlSchemaElement)
                    {
                        XmlSchemaElement xse = (XmlSchemaElement)obj;

                        schemas.Add(xse);
                        schemaNames.Add(xse.Name);
                        sElements.Add(new SchemaElement(schemaTargetNamesapce, xse.Name));
                    }
                }

                reader.Close();

                ArrayList result = new ArrayList();
                result.Add(schemas);
                result.Add(sElements);
                result.Add(schemaNames);

                return result;
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Error occurred while reading the schema file.", ex);
            }
            finally
            {
                if (reader != null && reader.ReadState != ReadState.Closed)
                {
                    reader.Close();
                }
            }
        }
    }
}

   
    
  
Related examples in the same category
1.Set XmlReaderSettings
2.Choose ValidationType
3.Validate an XML Document Against a Schema
4.Validate Schema
5.Use XML schema to validate XML documents
6.Use XmlReaderSettings to validate the Xml document
7.Strip Non Valid XML Characters.
8.Is Well Formed Xml
9.XmlSchema is an in-memory representation of an XML Schema
10.Is Xml Valid
11.Xml Validation Helper
12.Reads a XML schema file and returns the information found in that.
13.XML reading functionality
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.