get XMLSchemaTypes from XSD using XML in C#

Experts,
 I am TRYING to parse through XML and use the XSD to build a form
I can get types like SImpleTypeRescritions like Dates, and string
I can even (if i kow the element name already), build drop down boxes in HTML from Enumeraton or Patterns

what i want to be able to do is "Determine" when an attribute or element contains enumerations or Patterns (somethign i haven't been able to do

Can you help, please?

I am calling from an ASPX file.. ElementinSchema to start the process

I really would apprciate your hellp

thanks

Dan
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;

using System.Collections.Generic;
using System.IO;
//using System.Linq; 

namespace ssrf
{
    public partial class txrx : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }




        public void ProcessXml(string xmlPath)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(xmlPath);

            // Set up namespace manager for XPath    
            XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);



            nsMgr.AddNamespace("ddms", "urn:us:gov:dod:standard:ssrf:1.2.4.b");
            nsMgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
            nsMgr.AddNamespace("s", "urn:us:gov:dod:standard:ssrf:1.2.4.b");
            nsMgr.AddNamespace("my", "http://metadata.dod.mil/mdr/ns/SSRF/1.2.4.b/ssrf.xsd");


            XmlNodeList txrx2 = doc.SelectNodes("s:SSRF/s:Body/s:TxRx", nsMgr);

            foreach (XmlNode node in txrx2)
            {
                WalkNodes(node);
            }
        }

        private void WalkNodes(XmlNode iXmlNode)
        {
            if (iXmlNode.InnerText != null)
            {
                //Response.Write("Element - " + iXmlNode.Name + ": " + iXmlNode.InnerText + "<br>");
                Response.Write(iXmlNode.Name + "   <input type'input' name='" + iXmlNode.Name + "' value='" + iXmlNode.InnerText + "' size='25'><br>");
            }

            //Response.Write("inner XML -  " + iXmlNode.InnerXml + "<br>");
            if (iXmlNode.Attributes != null)
            {
                if (iXmlNode.Attributes.Count > 0)
                {
                    //Response.Write(iXmlNode.Name + " Attributes: " + "<br>");

                    XmlAttributeCollection attCol = iXmlNode.Attributes;

                    // Print them for testing
                    foreach (XmlAttribute sItem in attCol)
                    {
                        Response.Write(sItem.Name + "  <input type'input' name='" + sItem.Name + "' value='" + sItem.Value + "' size='25'><br>");

                    }
                }
            }

            foreach (XmlNode child in iXmlNode.ChildNodes)
            {
                WalkNodes(child);
            }

        }

        public void ElementInSchema()
        {
            XmlSchemaSet set = ReadSchemaSet();

            if (set == null)
            {
                Response.Write("File does not exist");

            }
            else
            {
                XmlQualifiedName name = new XmlQualifiedName("TStd", "urn:us:gov:dod:standard:ssrf:1.2.4.b");



                foreach (XmlSchema schema in set.Schemas())
                {

                    XmlSchemaObject item = FindElement(schema, name) as XmlSchemaObject;


                    if (item is XmlSchemaComplexType)
                    {
                        //Response.Write("THIS IS A COMPLEX TYPE<br>");

                        AttributeinSchema(schema, name);

                    }
                    if (item is XmlSchemaSimpleType)
                    {
                        XmlSchemaSimpleType value = FindElement(schema, name) as XmlSchemaSimpleType;

                        XmlSchemaSimpleTypeRestriction r = value.Content as XmlSchemaSimpleTypeRestriction;



                        if (r != null)
                        {
                            foreach (XmlSchemaFacet f in r.Facets)
                            {
                                XmlSchemaMinLengthFacet ml = f as XmlSchemaMinLengthFacet;
                                if (ml != null)
                                {
                                    Response.Write("min length=" + ml.Value + "<BR>");
                                }

                                XmlSchemaMaxLengthFacet m = f as XmlSchemaMaxLengthFacet;
                                if (m != null)
                                {
                                    Response.Write("max length=" + m.Value + "<BR>");
                                }
                            }
                        }
                    }
                }

            }
        }


        public void AttributeinSchema(XmlSchema schema, XmlQualifiedName name)
        {
            XmlSchemaComplexType value = FindElement(schema, name) as XmlSchemaComplexType;

            if (value.Attributes.Count != 0)
            {
                foreach (XmlSchemaAttribute atr in value.Attributes)
                {
                    
                    if (atr.SchemaTypeName.Name == "date")
                    {


                        Response.Write("<input type=\"Text\" name='" + atr.Name + "' value=''>");
                        Response.Write("<a href=\"javascript:show_calendar('document.tstest." + atr.Name + "', document.tstest." + atr.Name + ".value);\"><img src=\"cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Click Here to Pick up the timestamp\"></a>");

                        //  Response.Write(atr.Name + " " + atr.SchemaTypeName.Name + " " + atr.AttributeSchemaType + "<br>");

                    }
                    else
                    {
                        //Response.Write(atr.AttributeSchemaType.BaseSchemaType);
                        //getSimpleTypeElementinSchema(atr.SchemaTypeName.Name);
                       
                            Response.Write(atr.Name + " " + atr.SchemaTypeName.Name + " " + atr.AttributeSchemaType + "<br>");
                        
                    }
                }
            }

        }

        private void getSimpleTypeElementinSchema(string XSDQualName)
        {
            XmlSchemaSet set = ReadSchemaSet();

            if (set == null)
            {
                Response.Write("File does not exist");

            }
            else
            {
                //XmlQualifiedName name = new XmlQualifiedName("TStd", "c");
                XmlQualifiedName name = new XmlQualifiedName(XSDQualName, "urn:us:gov:dod:standard:ssrf:1.2.4.b");



                foreach (XmlSchema schema in set.Schemas())
                {

                    XmlSchemaObject item = FindElement(schema, name) as XmlSchemaObject;


                    if (item is XmlSchemaSimpleType)
                    {
                        XmlSchemaSimpleType value = FindElement(schema, name) as XmlSchemaSimpleType;

                        XmlSchemaSimpleTypeRestriction r = value.Content as XmlSchemaSimpleTypeRestriction;
                       
                        if (r != null)
                        {
                            foreach (XmlSchemaFacet f in r.Facets)
                            {
                                

                                //string tmpname = value.Name; 
                                Response.Write("value.name=" + value.Name + "<br>");
                               // if (value.Name = tmpname)
                                //{
                                Response.Write("f.value=" + f.Value + "<br>");

                                
                                //XmlSchemaEnumerationFacet enumM =  f as XmlSchemaEnumerationFacet;
                                //    if (enumM != null)
                                //    {
                                //        EnumInSchema(name);
                                //        //Response.Write("is Enum<br>");
                                //    }

                                    XmlSchemaMinLengthFacet ml = f as XmlSchemaMinLengthFacet;
                                    if (ml != null)
                                    {
                                        Response.Write("min length=" + ml.Value + "<BR>");
                                    }

                                    XmlSchemaMaxLengthFacet m = f as XmlSchemaMaxLengthFacet;
                                    if (m != null)
                                    {
                                        Response.Write("max length=" + m.Value + "<BR>");
                                    }
                                //}
                            }
                        }
                    }
                }

            }
        }

 

        public void PatternInSchema()
        {
            string patTemp;
            XmlSchemaSet set = ReadSchemaSet();

            XmlQualifiedName name = new XmlQualifiedName("ListAA", "urn:us:gov:dod:standard:ssrf:1.2.4.b");

            foreach (XmlSchema schema in set.Schemas())
            {
                XmlSchemaSimpleType value = FindElement(schema, name) as XmlSchemaSimpleType;

                XmlSchemaSimpleTypeRestriction r = value.Content as XmlSchemaSimpleTypeRestriction;
                Response.Write("<BR>");
                Response.Write(value.TypeCode + "<BR>");
                Response.Write(value.Name + "<BR>");
                if (r != null)
                {
                    foreach (XmlSchemaFacet f in r.Facets)
                    {
                        XmlSchemaPatternFacet p = f as XmlSchemaPatternFacet;
                        if (p != null)
                        {
                            //Response.Write(p.Value + "<BR>");
                            patTemp = p.Value;
                            string[] sPattern = patTemp.Split('|');
                            Response.Write("<select name='" + value.Name + "'>");
                            foreach (string word in sPattern)
                            {
                                // Console.WriteLine(word);
                                
                               
                                Response.Write("<option value='" + word + "'>" + word + "</option>");
                                
                            }
                            Response.Write(" </select>");
                            Response.Write(" <br><br> ");
                        }
                    }
                    
                }
            }

        }

        public void getElementInSchema()
        {

            XmlSchemaSet set = ReadSchemaSet();
            XmlQualifiedName name = new XmlQualifiedName("ListAL", "urn:us:gov:dod:standard:ssrf:1.2.4.b");


            foreach (XmlSchema schema in set.Schemas())
            {
                object item = FindElement(schema, name) as object;

                XmlSchemaSimpleType value = FindElement(schema, name) as XmlSchemaSimpleType;

                XmlSchemaSimpleTypeRestriction r = value.Content as XmlSchemaSimpleTypeRestriction;

                if (r != null)
                {
                    foreach (XmlSchemaFacet f in r.Facets)
                    {
                        XmlSchemaMaxLengthFacet m = f as XmlSchemaMaxLengthFacet;
                        if (m != null)
                        {
                            Response.Write("value - " + m.Value);
                        }
                    }
                }

            }


        }

        public void getschematest()
        {
            //string ls_path = HttpContext.Current.Server.MapPath("/");

           //string xsdname = ls_path + @"ssrf\ssrf\ssrf.xsd";

            //previewit/SSRF/SSRF
            string xsdname = @"D:\Hosting\4509985\html\previewit\SSRF\SSRF\ssrf.xsd";
            if (File.Exists(xsdname))
            {
                Response.Write("file exists at<br>");
                Response.Write(xsdname);
            }else{
                Response.Write("file does not exist<br>");
                Response.Write(xsdname);
            }
           
        }

        public void EnumInSchema(XmlQualifiedName name)
        {


             XmlSchemaSet set = ReadSchemaSet();

          //  XmlQualifiedName name = new XmlQualifiedName("ListAN", "urn:us:gov:dod:standard:ssrf:1.2.4.b");
            // XmlQualifiedName name = new XmlQualifiedName("XSDQName", "urn:us:gov:dod:standard:ssrf:1.2.4.b");
            foreach (XmlSchema schema in set.Schemas())
            {
                XmlSchemaSimpleType value = FindElement(schema, name) as XmlSchemaSimpleType;


                XmlSchemaSimpleTypeRestriction r = value.Content as XmlSchemaSimpleTypeRestriction;
                Response.Write(value.TypeCode + "<BR>");
                Response.Write(value.Name + "<BR>");
                Response.Write("<select name='" + value.Name + "'>");
                foreach (object item in schema.Items)
                {
                    Response.Write(item + "<BR>");
                }

                foreach (XmlSchemaFacet f in r.Facets)
                {
                    if (f is XmlSchemaEnumerationFacet)
                    {
                        XmlSchemaEnumerationFacet e = f as XmlSchemaEnumerationFacet;

                        if (e != null)
                        {
                            
                            Response.Write("<option value='" + f.Value + "'>" + f.Value + "</option>");
                           

                            //Response.Write(f.Value + " is XmlSchemaEnumerationFacet<BR>");
                        }
                    }
                }
                Response.Write(" </select>");
            }

        

        }



        static XmlSchemaType FindElement(XmlSchema schema, XmlQualifiedName name)
        {
            XmlSchemaType schemaType = schema.SchemaTypes[name] as XmlSchemaType;
            if (schemaType == null)
            {
                foreach (XmlSchemaType s in schema.SchemaTypes.Values)
                {
                    if (s.Name == name.Name)
                    {
                        schemaType = s;
                        break;
                    }
                }
            }
            if (schemaType != null)
            {
                return schemaType;
            }


            foreach (XmlSchemaObject o in schema.Includes)
            {
                XmlSchemaInclude include = o as XmlSchemaInclude;
                if (include != null)
                {
                    schemaType = FindElement(include.Schema, name);
                    if (schemaType != null)
                    {
                        return schemaType;
                    }
                }
            }
            return schemaType;
        }

       

        static XmlSchemaSet ReadSchemaSet()
        {
            XmlSchemaSet set = new XmlSchemaSet();

           //string ls_path = HttpContext.Current.Server.MapPath("/");

           //string xsdname = ls_path + @"ssrf\ssrf\ssrf.xsd";

            string xsdname = @"c:\itt\ssrf\ssrf\ssrf.xsd";
            //string xsdname =  @"D:\Hosting\4509985\html\previewit\SSRF\SSRF\ssrf.xsd";
            if (File.Exists(xsdname))
            {
                set.Add(null, xsdname);
                set.Compile();
            }else{
                set = null;
            }

            return set;
        }

    }
}

Open in new window

ssrf.zip
LVL 1
dsiemonAsked:
Who is Participating?
 
Meir RivkinConnect With a Mentor Full stack Software EngineerCommented:
please review my post in regard to schema parsing:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_25980147.html
0
 
Meir RivkinFull stack Software EngineerCommented:
if u need further assistant or post wasn't provide u with relevant info I'll be happy to help you.
0
 
Meir RivkinFull stack Software EngineerCommented:
@angelIII

if 'Delete with no refund', why points (at least some) are not assigned to me?
0
 
Meir RivkinFull stack Software EngineerCommented:
@Modalot
@AngelIII

seems this issue happens quite often when askers abandoned their posts.
"Delete - no refund" appears to me some kind of "warning sign" for askers not to abandon the question or else they lose the points anyway, but also it's unfair to the experts who spend time looking for solution for them.
on the same note, the experts get no reward for their work excused by a vaguely explanation  "cannot be certain about reaching any solution or help by the posts made by Experts".
this is purely coming from constructive place and i have no intention to gain rewards for free.
also my post here is from a general perspective and doesn't relate to this question.

cheers and thank you.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.