Hey guys, if im trying to parse a xsd file using the schema class how can i get the simpletype restrictions for a select attribute which i have already found.
so if its an enumeration type get the values, or if the patters in [a-zA-Z0-9] to get that.
public static ArrayList te = new ArrayList();
public static ArrayList Parser1(String _selected, String _next)
{
te = new ArrayList();
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add("http://www.example.com/B", "C:\\Users\\navid\\Desktop\\Validator\\XMLSchema3.xsd");
schemaSet.Compile();
XmlSchema xs = null;
ArrayList aa = new ArrayList();
foreach (XmlSchema schema in schemaSet.Schemas())
{
xs = schema;
}
foreach (XmlSchemaElement element in xs.Elements.Values)
{
if (element.Name.ToString().CompareTo("BookingList") == 0)
{
aa = findRequired(_selected, _next, xs);
}
}
return aa;
}
static ArrayList findRequired(String _type, String _next, XmlSchema xss)
{
foreach (XmlSchemaType type in xss.SchemaTypes.Values)
{
if (type is XmlSchemaComplexType)
{
XmlSchemaComplexType test = (XmlSchemaComplexType)type;
if (type.Name.ToString().CompareTo(_type) == 0)
{
foreach (XmlSchemaAttribute att in test.Attributes)
{
try
{
MessageBox.Show(att.SchemaType.BaseXmlSchemaType.Datatype.ValueType.Name.ToString() + " j");
MessageBox.Show(att.SchemaTypeName.Name.ToString().Length.ToString());
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
te.Add(att.QualifiedName.ToString());
}
XmlSchemaSequence sequence = test.ContentTypeParticle as XmlSchemaSequence;
// Iterate over each XmlSchemaElement in the Items collection.
try
{
foreach (XmlSchemaElement childElement in sequence.Items)
{
if (childElement.Name.ToString().CompareTo(_next) != 0)
{
findRequired(childElement.SchemaTypeName.Name.ToString(), _next, xss);
}
}
}
catch (Exception)
{
}
}
}
}
return te;
}
static void ValidationCallback(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
MessageBox.Show("WARNING: ");
else if (args.Severity == XmlSeverityType.Error)
MessageBox.Show("ERROR: ");
MessageBox.Show(args.Message);
}
}
}
This is the code i have so far.
The xsd looks like
<?xml version="1.0" encoding="utf-8"?>
<schema xmlns ="http://www.w3.org/2001/XMLSchema"
targetNamespace ="http://www.example.com/B"
elementFormDefault="qualified"
xmlns:b="http://www.example.com/B">
<element name ="BookingList" type="b:BookingList"/>
<complexType name="BookingList">
<sequence>
<element name="Booking" type="b:Booking" minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
<complexType name="Booking">
<sequence>
<element name="BookedLineItem" type="b:BookedLineItem" minOccurs="1" maxOccurs="unbounded"/>
</sequence>
<attribute name="VoyageID" use="required" type="string" />
<attribute name="BookingNumber" use="required" type="string"/>
<attribute name="LoadPort" use="required" type="string"/>
<attribute name="DischargePort" use="required" type="string"/>
</complexType>
<complexType name="BookedLineItem">
<sequence>
<element name="RegCargoClass" type="b:RegCargoClass" minOccurs="1" />
<element name="Commodity" type="b:Commodity" minOccurs ="1" />
<element name="AmountType" type="b:AmountType" minOccurs="1" />
</sequence>
<attribute name="BookedLineItem" use="required"/>
</complexType>
<complexType name ="RegCargoClass">
<attribute name="Cargo" use="required">
<simpleType>
<restriction base="string">
<enumeration value="Container"/>
<enumeration value="Auto"/>
<enumeration value="RoRo"/>
<enumeration value="BreakBulk"/>
<enumeration value="Equipment"/>
</restriction>
</simpleType>
</attribute>
</complexType>
<complexType name="AmountType">
<attribute name="AmountType" use="required" >
<simpleType>
<restriction base="string">
<pattern value="M|U"/>
</restriction>
</simpleType>
</attribute>
<attribute name="Quantity" use="required" type="integer"/>
<attribute name="Mass" use="required" type="integer"/>
<attribute name="Length" use="required" type="integer"/>
<attribute name="Width" use ="required" type="integer"/>
<attribute name="Height" use="required" type="integer"/>
</complexType>
<complexType name="Commodity">
<attribute name="CommodityCode" use="required" type="string"/>
<attribute name="CommodityDescription" use="required" type="string"/>
</complexType>
</schema>