Showing posts with label Read XSD file. Show all posts
Showing posts with label Read XSD file. Show all posts

Friday, October 9, 2009

How to Gather data from XSD file to use the values in c# code behind.

Sippose following is your xsd File

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="XMLSchema"
elementFormDefault="qualified"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="DeliverSchType">
<xs:restriction base="xs:int">
<xs:enumeration value="01" />
<xs:enumeration value="02" />
<xs:enumeration value="08" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="DeliverSchType1">
<xs:restriction base="xs:int">
<xs:enumeration value="01" />
<xs:enumeration value="02" />
<xs:enumeration value="08" />
</xs:restriction>
</xs:simpleType>
</xs:schema>


Now assume that from above you need all the enum values from DeliverSchType1 simple type in your code behind.


To do so you can write following c# code.

string fileName = Server.MapPath("XMLSchema.xsd");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(fileName);
XmlNamespaceManager xMan = new XmlNamespaceManager(xDoc.NameTable);
xMan.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");

XmlNodeList xNodeList = xDoc.SelectNodes(@"xs:schema/xs:simpleType[@name = 'DeliverSchType']/xs:restriction/xs:enumeration", xMan);

string[] enumVal = new string[xNodeList.Count];
int ctr = 0;
foreach (XmlNode xNode in xNodeList)
{
enumVal[ctr] = xNode.Attributes["value"].Value;
ctr++;

}

In enumVal[] you will get all the data of DeliverSchType1 simple type as ,
01
02
08