Thursday, February 20, 2014

Edit xsd file and Update using C# code behind

I am working in Windows destop application .net blatform in wpf advance technology. i want to update my xsd validation schema file using Csharp code behaind. I want to update my xsd file simpleType ("XmlSchemaSimpleType") tag inside Add new enumeration ("XmlSchemaEnumerationFacet"). If there is no tag I want to add one new tag. If there is already more then one tag I want to add additional tag. If already exist the same value tag I will try to add that can be retrun; Its very simple to this sample code. This is my book1.xsd file:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="urn:bookstore-schema" targetNamespace="urn:bookstore-schema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="NameType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Actor" />
      <xs:enumeration value="Producer" />
      <xs:enumeration value="Senthil" />
      <xs:enumeration value="Kumar" />
      <xs:enumeration value="navamani1" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="EmployeeType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Worker" />
      <xs:enumeration value="Manager" />
      <xs:enumeration value="Admin" />
      <xs:enumeration value="Sales" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
this is the method update the xsd file.
[STAThread]
static void Main(string[] args)
{
 UpdateValidationSchema("book1.xsd", "navamani1");
}

 public static void UpdateValidationSchema(string validationSchemaPath, string stationName)
        {
            XmlSchema schema;

            using (var reader = new StreamReader(validationSchemaPath))
            {
                schema = XmlSchema.Read(reader, null);
            }

            // Compile so that post-compilation information is available
            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.Add(schema);
            schemaSet.Compile();

            // Update schema reference
            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 enumFacets = restriction.Facets.OfType<XmlSchemaEnumerationFacet>();

                if (enumFacets.Any())
                {
                    foreach (var facet in enumFacets)
                    {
                        string exitStationName = facet.Value;

                        if (exitStationName == stationName)
                        {
                            return;
                        }
                    }
                }

                string validStationType = simpleType.Name;

                if (validStationType == "NameType")
                {
                    XmlSchemaEnumerationFacet staionEnum =
                            new XmlSchemaEnumerationFacet();

                    staionEnum.Value = stationName;
                    restriction.Facets.Add(staionEnum);
                }
            }

            StreamWriter streamWriter = new StreamWriter(validationSchemaPath, false);
            schema.Write(streamWriter);
        }

No comments:

Post a Comment