Xsd模式生成 - 未声明类型“字符串”,或者不是简单类型

时间:2014-02-06 09:46:17

标签: c# xml xsd

我正在使用架构对象模型创建一个xml架构,在步骤“set.compile()”上,我得到了上面提到的错误。我想它与simpletype元素的基本类型名称相关,帮助!

有没有方法,我们实际上可以看到什么是giong而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();

1 个答案:

答案 0 :(得分:0)

尝试使用名称空间的全名:

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

请参阅第http://msdn.microsoft.com/en-us/library/1c4sky9s(v=vs.110).aspx页上的示例部分