在XML模式中创建嵌套的XmlSchemaComplexType

时间:2015-01-27 09:28:04

标签: c# xml xsd

我想创建一个xsd文件,例如this

我正在使用此

            XmlSchema schema = new XmlSchema();
            schema.Id = "SHP_CAHPS_HOSPICE_DATA";

            XmlSchemaElement elementData = new XmlSchemaElement();
            schema.Items.Add(elementData);
            elementData.Name = "SHP_CAHPS_HOSPICE_DATA";


            XmlSchemaComplexType complexType = new XmlSchemaComplexType();
            elementData.SchemaType = complexType;// This is same as below. It's working fine but next is not working.

            XmlSchemaSequence sequence = new XmlSchemaSequence();
            complexType.Particle = sequence;

            // here some elements

            XmlSchemaElement surveydataEle = new XmlSchemaElement();
            sequence.Items.Add(surveydataEle);
            surveydataEle.Name = "CAHPS_HOSPICE_SURVEY_DATA";
            surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
            surveydataEle.MinOccurs = 1;
            surveydataEle.MaxOccurs = 2000;

            XmlSchemaComplexType complexType1 = new XmlSchemaComplexType();
            surveydataEle.SchemaType = complexType1;

            XmlSchemaSequence sequence1 = new XmlSchemaSequence();
            complexType1.Particle = sequence1;

            Type type = patient.GetType();
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                string name = property.Name;
                XmlSchemaElement element = new XmlSchemaElement();
                sequence1.Items.Add(element);
                element.Name = name;
                element.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
                element.MinOccurs = 0;
            }

            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);
            schemaSet.Add(schema);
            schemaSet.Compile();

            XmlSchema compiledSchema = null;

            foreach (XmlSchema schema1 in schemaSet.Schemas())
            {
                compiledSchema = schema1;
            }
            Random r = new Random();
            int random = r.Next();
            string filePath = "D:\\" + random + ".xsd";
            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
            }
            //Stream stream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(filePath, new UTF8Encoding());
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
            nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
            compiledSchema.Write(writer, nsmgr); // Exception is occurring here 
            writer.Flush();
            writer.Close();

这不行。 Object reference not set to an instance of an object

发生例外compiledSchema.Write(writer, nsmgr);

当我没有使用第二个XmlSchemaComplexType时 它工作正常。在这里,我使用相同的方式添加XmlSchemaComplexType,这是我使用拳头,但第二次它不起作用。

请建议我。

1 个答案:

答案 0 :(得分:1)

因为NullReferenceException为空而引发compiledSchema,这是因为schemaSet.Schemas()是一个空集合,这是因为schemaSet.Add(schema)失败,这是因为您的架构是碎。

您的架构被破坏的原因是您声明CAHPS_HOSPICE_SURVEY_DATA既是字符串又是复杂类型,可以通过从schema变量转储XML来看到:

<?xml version="1.0" encoding="utf-16"?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="SHP_CAHPS_HOSPICE_DATA" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="SHP_CAHPS_HOSPICE_DATA">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element minOccurs="1" maxOccurs="2000" name="CAHPS_HOSPICE_SURVEY_DATA" type="xsd:string">
                    <xsd:complexType>
                        <xsd:sequence>

您使用SchemaTypeName声明它是一个字符串:

        surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

然后使用SchemaType将其声明为复杂类型:

        surveydataEle.SchemaType = complexType1;

修复是删除第一个声明:

        XmlSchemaElement surveydataEle = new XmlSchemaElement();
        sequence.Items.Add(surveydataEle);
        surveydataEle.Name = "CAHPS_HOSPICE_SURVEY_DATA";
        // surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");  <-- Deleted this line.
        surveydataEle.MinOccurs = 1;
        surveydataEle.MaxOccurs = 2000;

        XmlSchemaComplexType complexType1 = new XmlSchemaComplexType();
        surveydataEle.SchemaType = complexType1;