4

I have two types of XSD file.Below are the two types of XSD

Type 1

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Body">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="value">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ConcurrencyToken" />
              <xs:element name="Id" type="xs:string" />
              <xs:element name="Location" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="odata.context" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>

Type 2

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Body">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ConcurrencyToken" />
        <xs:element name="Id" type="xs:string" />
        <xs:element name="CatalogName" type="xs:string" />
        <xs:element name="SchemaName" type="xs:string" />
        <xs:element name="NameService" type="xs:string" />
      </xs:sequence>
      <xs:attribute name="odata.context" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>

This is my code

 public static List<SchemaStructure> XsdNestedParser(string schemaPath)
        {
            List<SchemaStructure> schemaStructList = new List<SchemaStructure>();

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

            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.Add(xmlSchema);
            schemaSet.Compile();
            xmlSchema = schemaSet.Schemas().Cast<XmlSchema>().First();
            foreach (XmlSchemaElement element in xmlSchema.Elements.Values)
            {
                XmlSchemaComplexType complexTypeRoot = element.ElementSchemaType as XmlSchemaComplexType;
                XmlSchemaSequence sequenceRoot = complexTypeRoot.ContentTypeParticle as XmlSchemaSequence;
                foreach (object childElement in sequenceRoot.Items)
                {
                    XmlSchemaElement schemaElement = childElement as XmlSchemaElement;
                    XmlSchemaComplexType complexType = schemaElement.ElementSchemaType as XmlSchemaComplexType;
                    XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;
                    if (complexType != null)
                    {
                        for (int i = 0; i < sequence.Items.Count; i++)
                        {
                            XmlSchemaElement child = sequence.Items[i] as XmlSchemaElement;
                            XmlSchemaChoice choice = sequence.Items[i] as XmlSchemaChoice;
                            string propertyName = String.Empty;
                            string dataType = string.Empty;

                            if (child != null)
                            {
                                propertyName = child.Name;
                                dataType = child.SchemaTypeName.Name;
                                SchemaStructure schemaStructure = new SchemaStructure(propertyName, dataType);
                                schemaStructList.Add(schemaStructure);
                            }
                            else
                            {
                                //Here I am getting nullreference exception while parsing type 2,however if it is type 2 it should go to else
                                for (int j = 0; j < choice.Items.Count; j++)
                                {
                                    XmlSchemaElement schemElement = choice.Items[j] as XmlSchemaElement;
                                    propertyName = schemElement.Name;
                                    dataType = schemElement.SchemaTypeName.Name;
                                    SchemaStructure schemaStructure = new SchemaStructure(propertyName, dataType);
                                    schemaStructList.Add(schemaStructure);
                                }
                            }
                        }
                    }
                    else
                    {

                        string propertyName = schemaElement.Name;
                        string dataType = schemaElement.SchemaTypeName.Name;
                        SchemaStructure schemaStructure = new SchemaStructure(propertyName, dataType);
                        schemaStructList.Add(schemaStructure);

                    }
                }
            }

            return schemaStructList;
        }

I am able to extract type 1, but I am getting the Null exception while extracting type 2.Where am I doing wrong?

Thanks in Advance

Mahek
  • 552
  • 2
  • 7
  • 28
  • XSDs are described by a XSD, so you can create a class to serialize/deserialize them. Can't you go that way? (It is possible that I misunderstood your question) – Matteo Umili Jan 04 '17 at 10:00
  • 2
    You checking if `child` variable is null but you not checking this for `choice` variable. – Renatas M. Jan 04 '17 at 10:04
  • Where does the Null exception occur? – PaulF Jan 04 '17 at 10:09
  • A schema (xsd) is a file use to validate an xml and doesn't contain data. there a a number of net library class that will check a xml file against the xsd schema. So I don't know why you are trying to reinvent the wheel. You should be extracting the data from the xml, not the xsd. – jdweng Jan 04 '17 at 10:11
  • I don't want to validate XML, purpose is to extract data type of particular property...@jdweng – Mahek Jan 04 '17 at 10:20
  • After your second "foreach" you can see that childElement.ElementType is null for type 2, whereas for type 1 it is an XmlSchemaComplexType. Maybe that is what you need to check. – PaulF Jan 04 '17 at 10:28
  • for type 2, complexType!=null should be false, as it will be null, however it is going into the if block instead of else.@PaulF – Mahek Jan 04 '17 at 10:39
  • I have Resharper installed & that highlights the else statement after _if(complexType!=null)_ as unreacheable code. Put a break point after _foreach (object childElement in sequenceRoot.Items)_ and examine the fields of childElement for both type1 & type 2 - you will see in both instances "ElementSchemaType" is not null (it is XmlSchemaComplexType), but "ElementType" is not null for type 1 & is null for type 2. – PaulF Jan 04 '17 at 10:47
  • I found my mistake ...thank you all..@PaulF – Mahek Jan 04 '17 at 10:56
  • Checking the documentation I see that "ElementType" is deprecated, but "SchemaType" is not & that acts the same - non-null for type 1, null for type 2. – PaulF Jan 04 '17 at 10:57

0 Answers0