C# Xsd模式生成-类型';字符串';未声明,或不是简单类型

C# Xsd模式生成-类型';字符串';未声明,或不是简单类型,c#,xml,xsd,C#,Xml,Xsd,我正在使用模式对象模型创建一个xml模式,在步骤“set.compile()”中,我得到了上面提到的错误。我猜它与simpletype元素的基类型名有关,请帮助 有没有什么方法,我们可以在set.compile()方法编译代码的时候看到它到底发生了什么 XmlSchemaSimpleType namesimpletype = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeRestriction res = new Xm

我正在使用模式对象模型创建一个xml模式,在步骤“set.compile()”中,我得到了上面提到的错误。我猜它与simpletype元素的基类型名有关,请帮助

有没有什么方法,我们可以在set.compile()方法编译代码的时候看到它到底发生了什么

        XmlSchemaSimpleType namesimpletype = new XmlSchemaSimpleType();
        XmlSchemaSimpleTypeRestriction res = new XmlSchemaSimpleTypeRestriction();
        res.BaseTypeName = new XmlQualifiedName("string","");
        XmlSchemaMinLengthFacet facet1 = new XmlSchemaMinLengthFacet();
        facet1.Value = "3";
        XmlSchemaMaxLengthFacet facet2 = new XmlSchemaMaxLengthFacet();
        facet2.Value = "10";
        res.Facets.Add(facet1);
        res.Facets.Add(facet2);
        namesimpletype.Content = res;


        XmlSchemaSimpleType phonetype = new XmlSchemaSimpleType();
        XmlSchemaSimpleTypeRestriction phone_res = new XmlSchemaSimpleTypeRestriction();
        phone_res.BaseTypeName = new XmlQualifiedName("string", "");
        XmlSchemaMinLengthFacet phone_facet1 = new XmlSchemaMinLengthFacet();
        phone_facet1.Value = "4";
        XmlSchemaMaxLengthFacet phone_facet2 = new XmlSchemaMaxLengthFacet();
        phone_facet2.Value = "20";
        phone_res.Facets.Add(phone_facet1);
        phone_res.Facets.Add(phone_facet2);
        phonetype.Content = phone_res;


        XmlSchemaSimpleType notetype = new XmlSchemaSimpleType();
        XmlSchemaSimpleTypeRestriction note_res = new XmlSchemaSimpleTypeRestriction();
        note_res.BaseTypeName = new XmlQualifiedName("string", "");
        XmlSchemaMinLengthFacet note_facet1 = new XmlSchemaMinLengthFacet();
        note_facet1.Value = "0";
        XmlSchemaMaxLengthFacet note_facet2 = new XmlSchemaMaxLengthFacet();
        facet2.Value = "50";
        note_res.Facets.Add(note_facet1);
        note_res.Facets.Add(note_facet2);
        notetype.Content = note_res;



        XmlSchemaComplexType employeetype = new XmlSchemaComplexType();
        XmlSchemaSequence emp_seq = new XmlSchemaSequence();

        XmlSchemaElement firstname = new XmlSchemaElement();
        firstname.Name = "firstname";
        firstname.SchemaType = namesimpletype;

        XmlSchemaElement lastname = new XmlSchemaElement();
        lastname.Name = "lastname";
        lastname.SchemaType = namesimpletype;

        XmlSchemaElement homephone = new XmlSchemaElement();
        homephone.Name = "homePhone";
        homephone.SchemaType = phonetype;

        XmlSchemaElement notes = new XmlSchemaElement();
        notes.Name = "notes";
        notes.SchemaType = notetype;


        emp_seq.Items.Add(firstname);
        emp_seq.Items.Add(lastname);
        emp_seq.Items.Add(homephone);
        emp_seq.Items.Add(notes);

        employeetype.Particle = emp_seq;

       /* XmlSchemaAttribute employeeid = new XmlSchemaAttribute();
        employeeid.Name = "employeeid";
        employeeid.SchemaTypeName = new XmlQualifiedName("int", "");
        employeeid.Use = XmlSchemaUse.Required;
        employeetype.Attributes.Add(employeeid);
        */

        XmlSchemaComplexType complextype = new XmlSchemaComplexType();
        complextype.Name = "employees";

        XmlSchemaElement employee = new XmlSchemaElement();
        employee.Name = "employee";
        employee.SchemaType = employeetype;
        employee.MinOccurs = 0;
        employee.MaxOccursString = "unbounded"; 
        XmlSchemaSequence emps_seq = new XmlSchemaSequence();

        emps_seq.Items.Add(employee);

        complextype.Particle = emps_seq;
        XmlSchema schema = new XmlSchema();
        schema.Items.Add(complextype);


        XmlSchemaSet set = new XmlSchemaSet();
        set.Add(schema);

        set.Compile();

       /* try
        {
            set.Compile();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }  */
        FileStream stream = new FileStream(textBox1.Text, FileMode.Open);

        XmlTextWriter writer = new XmlTextWriter(stream, null);
        schema.Write(writer);
        writer.Close();

尝试在命名空间中使用全名:

res.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
请参见第页的示例部分