我有两种类型的XSD文件。下面是两种类型的XSD
输入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>
类型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>
这是我的代码
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;
}
我能够提取类型1,但是在提取类型2时我得到了Null异常。我做错了什么?
先谢谢