Monday, February 24, 2014

Create Xsd file Schema using C# code behind

I am working in .net platform wpf technology. I have task in Create new xsd file using Csharp. When i created this xsd file i have so much troble for create this file.

after that i had same idea to create this xsd file. Its working file for the parporse i created this method, my xml file format validation only i have just return the value XmlSchema.

If you want to Create new file just use the four line Start the "FileStream" object till the "writer.Close()" close object.

If you don't need to save the xsd file just remove this four lines.

Now this is the method to create xsd file for xml validation schema format file.


public XmlSchema CreateXSDSchemaValidationFormate()
        {
            try
            {
                XmlSchemaSimpleType attriLeSimpleType = new XmlSchemaSimpleType();
                XmlSchemaSimpleTypeRestriction attriLeRestriction = new XmlSchemaSimpleTypeRestriction();
                attriLeRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
                XmlSchemaMinLengthFacet attriLeFacet = new XmlSchemaMinLengthFacet();
                attriLeFacet.Value = "1";
                attriLeRestriction.Facets.Add(attriLeFacet);
                attriLeSimpleType.Content = attriLeRestriction;

                XmlSchemaSimpleType attriIntLeSimpleType = new XmlSchemaSimpleType();
                XmlSchemaSimpleTypeRestriction attriIntLeRestriction = new XmlSchemaSimpleTypeRestriction();
                attriIntLeRestriction.BaseTypeName = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");
                XmlSchemaMinInclusiveFacet attriInclusiveFacet = new XmlSchemaMinInclusiveFacet();
                attriInclusiveFacet.Value = "0";
                attriIntLeRestriction.Facets.Add(attriInclusiveFacet);
                attriIntLeSimpleType.Content = attriIntLeRestriction;

                XmlSchemaComplexType proComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute proAttribut = new XmlSchemaAttribute();
                proAttribut.Name = "value";
                proAttribut.Use = XmlSchemaUse.Required;
                proAttribut.SchemaType = attriLeSimpleType;
                proComplex.Attributes.Add(proAttribut);

                XmlSchemaComplexType faComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute faAttribut = new XmlSchemaAttribute();
                faAttribut.Name = "value";
                faAttribut.Use = XmlSchemaUse.Required;
                faComplex.Attributes.Add(faAttribut);
                faAttribut.SchemaType = attriLeSimpleType;

                XmlSchemaComplexType lComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute lAttribut = new XmlSchemaAttribute();
                lAttribut.Name = "value";
                lAttribut.Use = XmlSchemaUse.Required;
                lAttribut.SchemaType = attriLeSimpleType;
                lComplex.Attributes.Add(lAttribut);

                XmlSchemaComplexType sComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute sAttribut = new XmlSchemaAttribute();
                sAttribut.Name = "value";
                sAttribut.Use = XmlSchemaUse.Required;
                sAttribut.SchemaType = attriLeSimpleType;
                sComplex.Attributes.Add(sAttribut);

                XmlSchemaComplexType stationIDElementComplex = new XmlSchemaComplexType();
                XmlSchemaSequence stationIDElementSequence = new XmlSchemaSequence();

                XmlSchemaElement producer = new XmlSchemaElement();
                producer.Name = "producer";
                producer.SchemaType = proComplex;

                XmlSchemaElement facility = new XmlSchemaElement();
                facility.Name = "facility";
                facility.SchemaType = faComplex;

                XmlSchemaElement line = new XmlSchemaElement();
                line.Name = "Line";
                line.SchemaType = lComplex;

                XmlSchemaElement station = new XmlSchemaElement();
                station.Name = "Station";
                station.SchemaType = sComplex;

                stationIDElementSequence.Items.Add(producer);
                stationIDElementSequence.Items.Add(facility);
                stationIDElementSequence.Items.Add(line);
                stationIDElementSequence.Items.Add(station);

                XmlSchemaComplexType senderConfigElementComplex = new XmlSchemaComplexType();
                XmlSchemaSequence senderConfigElementSequence = new XmlSchemaSequence();

                XmlSchemaComplexType eventIncomingRootAttributeComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute eventIncomingRootAttribute = new XmlSchemaAttribute();
                eventIncomingRootAttribute.Name = "value";
                eventIncomingRootAttribute.Use = XmlSchemaUse.Required;
                eventIncomingRootAttribute.FixedValue = @"C:\Document\Incoming\";
                eventIncomingRootAttribute.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
                eventIncomingRootAttributeComplex.Attributes.Add(eventIncomingRootAttribute);

                XmlSchemaElement eventIncomingRoot = new XmlSchemaElement();
                eventIncomingRoot.Name = "EventIncomingRoot";
                eventIncomingRoot.SchemaType = eventIncomingRootAttributeComplex;

                XmlSchemaComplexType eventErrorRootAttributeComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute eventErrorRootAttribute = new XmlSchemaAttribute();
                eventErrorRootAttribute.Name = "value";
                eventErrorRootAttribute.Use = XmlSchemaUse.Required;
                eventErrorRootAttribute.FixedValue = @"C:\Document\Errors\";
                eventErrorRootAttribute.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
                eventErrorRootAttributeComplex.Attributes.Add(eventErrorRootAttribute);

                XmlSchemaElement eventErrorRoot = new XmlSchemaElement();
                eventErrorRoot.Name = "EventErrorRoot";
                eventErrorRoot.SchemaType = eventErrorRootAttributeComplex;

                XmlSchemaComplexType eventInProcessRootAttributeComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute eventInProcessRootAttribute = new XmlSchemaAttribute();
                eventInProcessRootAttribute.Name = "value";
                eventInProcessRootAttribute.Use = XmlSchemaUse.Required;
                eventInProcessRootAttribute.FixedValue = @"C:\Document\InProcess\";
                eventInProcessRootAttribute.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
                eventInProcessRootAttributeComplex.Attributes.Add(eventInProcessRootAttribute);

                XmlSchemaElement eventInProcessRoot = new XmlSchemaElement();
                eventInProcessRoot.Name = "EventInProcessRoot";
                eventInProcessRoot.SchemaType = eventInProcessRootAttributeComplex;

                XmlSchemaComplexType eventToBeDeletedRootAttributeComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute eventToBeDeletedRootAttribute = new XmlSchemaAttribute();
                eventToBeDeletedRootAttribute.Name = "value";
                eventToBeDeletedRootAttribute.Use = XmlSchemaUse.Required;
                eventToBeDeletedRootAttribute.FixedValue = @"C:\Document\ToBeDeleted\";
                eventToBeDeletedRootAttribute.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
                eventToBeDeletedRootAttributeComplex.Attributes.Add(eventToBeDeletedRootAttribute);

                XmlSchemaElement eventToBeDeletedRoot = new XmlSchemaElement();
                eventToBeDeletedRoot.Name = "EventToBeDeletedRoot";
                eventToBeDeletedRoot.SchemaType = eventToBeDeletedRootAttributeComplex;

                XmlSchemaComplexType eventXmitRetryRootAttributeComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute eventXmitRetryRootAttribute = new XmlSchemaAttribute();
                eventXmitRetryRootAttribute.Name = "value";
                eventXmitRetryRootAttribute.Use = XmlSchemaUse.Required;
                eventXmitRetryRootAttribute.FixedValue = @"C:\Document\XmitRetry\";
                eventXmitRetryRootAttribute.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
                eventXmitRetryRootAttributeComplex.Attributes.Add(eventXmitRetryRootAttribute);

                XmlSchemaElement eventXmitRetryRoot = new XmlSchemaElement();
                eventXmitRetryRoot.Name = "EventXmitRetryRoot";
                eventXmitRetryRoot.SchemaType = eventXmitRetryRootAttributeComplex;

                XmlSchemaComplexType userIDAttributeComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute userIDAttribute = new XmlSchemaAttribute();
                userIDAttribute.Name = "value";
                userIDAttribute.Use = XmlSchemaUse.Required;
                userIDAttribute.SchemaType = attriLeSimpleType;
                userIDAttributeComplex.Attributes.Add(userIDAttribute);

                XmlSchemaComplexType accessKeyAttributeComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute accessKeyAttribute = new XmlSchemaAttribute();
                accessKeyAttribute.Name = "value";
                accessKeyAttribute.Use = XmlSchemaUse.Required;
                accessKeyAttribute.SchemaType = attriLeSimpleType;
                accessKeyAttributeComplex.Attributes.Add(accessKeyAttribute);

                XmlSchemaComplexType eeceiverServiceElementComplex = new XmlSchemaComplexType();
                XmlSchemaSequence eeceiverServiceElementSequence = new XmlSchemaSequence();

                XmlSchemaElement userID = new XmlSchemaElement();
                userID.Name = "UserID";
                userID.SchemaType = userIDAttributeComplex;

                XmlSchemaElement accessKey = new XmlSchemaElement();
                accessKey.Name = "AccessKey";
                accessKey.SchemaType = accessKeyAttributeComplex;

                eeceiverServiceElementSequence.Items.Add(userID);
                eeceiverServiceElementSequence.Items.Add(accessKey);

                eeceiverServiceElementComplex.Particle = eeceiverServiceElementSequence;

                XmlSchemaElement eeceiverService = new XmlSchemaElement();
                eeceiverService.Name = "ReceiverService";
                eeceiverService.SchemaType = eeceiverServiceElementComplex;

                XmlSchemaComplexType eventPollingIntervalAttributesComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute eventPollingIntervalAttributes = new XmlSchemaAttribute();
                eventPollingIntervalAttributes.Name = "value";
                eventPollingIntervalAttributes.Use = XmlSchemaUse.Required;
                eventPollingIntervalAttributes.SchemaType = attriIntLeSimpleType;
                eventPollingIntervalAttributesComplex.Attributes.Add(eventPollingIntervalAttributes);

                XmlSchemaElement eventPollingInterval = new XmlSchemaElement();
                eventPollingInterval.Name = "EventPollInterval";
                eventPollingInterval.SchemaType = eventPollingIntervalAttributesComplex;

                XmlSchemaComplexType transmissionIntervalsElementComplex = new XmlSchemaComplexType();
                XmlSchemaSequence transmissionIntervalsElementSequence = new XmlSchemaSequence();

                XmlSchemaElement transmissionIntervals = new XmlSchemaElement();
                transmissionIntervals.Name = "TransIntervals";
                transmissionIntervals.SchemaType = transmissionIntervalsElementComplex;

                XmlSchemaComplexType intervalAttributesComplex = new XmlSchemaComplexType();
                XmlSchemaSequence emp_seq5 = new XmlSchemaSequence();

                XmlSchemaElement interval = new XmlSchemaElement();
                interval.Name = "Interval";
                interval.MaxOccurs = 0;
                interval.MaxOccursString = "unbounded";
                interval.SchemaType = intervalAttributesComplex;

                XmlSchemaAttribute intervalAttributes = new XmlSchemaAttribute();
                intervalAttributes.Name = "AfterFailures";
                intervalAttributes.Use = XmlSchemaUse.Required;
                intervalAttributes.SchemaType = attriIntLeSimpleType;
                intervalAttributesComplex.Attributes.Add(intervalAttributes);

                XmlSchemaAttribute intervalAttributes2 = new XmlSchemaAttribute();
                intervalAttributes2.Name = "value";
                intervalAttributes2.Use = XmlSchemaUse.Required;
                intervalAttributes2.SchemaType = attriIntLeSimpleType;
                intervalAttributesComplex.Attributes.Add(intervalAttributes2);

                transmissionIntervalsElementSequence.Items.Add(interval);
                transmissionIntervalsElementComplex.Particle = transmissionIntervalsElementSequence;

                XmlSchemaComplexType batchSizeLimitKBAttributesComplex = new XmlSchemaComplexType();

                XmlSchemaAttribute batchSizeLimitKBAttributes = new XmlSchemaAttribute();
                batchSizeLimitKBAttributes.Name = "value";
                batchSizeLimitKBAttributes.Use = XmlSchemaUse.Required;
                batchSizeLimitKBAttributes.SchemaType = attriIntLeSimpleType;
                batchSizeLimitKBAttributesComplex.Attributes.Add(batchSizeLimitKBAttributes);

                XmlSchemaElement batchSizeLimitKB = new XmlSchemaElement();
                batchSizeLimitKB.Name = "BatchSizeLimit";
                batchSizeLimitKB.SchemaType = batchSizeLimitKBAttributesComplex;

                senderConfigElementSequence.Items.Add(eventIncomingRoot);
                senderConfigElementSequence.Items.Add(eventErrorRoot);
                senderConfigElementSequence.Items.Add(eventInProcessRoot);
                senderConfigElementSequence.Items.Add(eventToBeDeletedRoot);
                senderConfigElementSequence.Items.Add(eventXmitRetryRoot);
                senderConfigElementSequence.Items.Add(eeceiverService);
                senderConfigElementSequence.Items.Add(eventPollingInterval);
                senderConfigElementSequence.Items.Add(transmissionIntervals);
                senderConfigElementSequence.Items.Add(batchSizeLimitKB);

                XmlSchemaElement senderConfig = new XmlSchemaElement();
                senderConfig.Name = "SenderConfig";
                senderConfig.SchemaType = senderConfigElementComplex;


                XmlSchemaComplexType stationConfigComplex = new XmlSchemaComplexType();
                XmlSchemaSequence stationConfigElementSquence = new XmlSchemaSequence();

                XmlSchemaElement stationID = new XmlSchemaElement();
                stationID.Name = "StationID";
                stationID.SchemaType = stationIDElementComplex;

                stationConfigElementSquence.Items.Add(stationID);
                stationConfigElementSquence.Items.Add(senderConfig);

                senderConfigElementComplex.Particle = senderConfigElementSequence;
                stationIDElementComplex.Particle = stationIDElementSequence;
                stationConfigComplex.Particle = stationConfigElementSquence;

                XmlSchemaElement stationConfig = new XmlSchemaElement();
                stationConfig.Name = "Config";
                stationConfig.SchemaType = stationConfigComplex;
                //employee.MinOccurs = 0;
                //employee.MaxOccursString = "unbounded";
                XmlSchemaSequence stationConfigSequence = new XmlSchemaSequence();

                stationConfigSequence.Items.Add(stationConfig);

                //employee.Particle = emps_seq;
                XmlSchema schema = new XmlSchema();
                schema.ElementFormDefault = XmlSchemaForm.Qualified;
                schema.AttributeFormDefault = XmlSchemaForm.Unqualified;
                schema.Items.Add(stationConfig);


                XmlSchemaSet set = new XmlSchemaSet();
                set.Add(schema);

                //if you want to create a new xsd file just pass the url;
                FileStream stream = new FileStream("c:\\NewSchema.xsd", FileMode.Open);

                XmlTextWriter writer = new XmlTextWriter(stream, null);
                schema.Write(writer);
                writer.Close();

                //if you want to use it for the xml validation just return this schema.
                return schema;
            }

            catch (XmlException XMLExp)
            {
                Console.WriteLine(XMLExp.Message);
            }
            catch (XmlSchemaException XmlSchemaExp)
            {
                Console.WriteLine(XmlSchemaExp.Message);
            }
            catch (Exception GenExp)
            {
                Console.WriteLine(GenExp.Message);
            }
            finally
            {
                Console.Read();
            }

            return null;
        }

No comments:

Post a Comment