65.9K
CodeProject 正在变化。 阅读更多。
Home

管理 XML 架构文件(添加元素 - 属性)

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.77/5 (5投票s)

2007年8月8日

viewsIcon

36593

downloadIcon

276

如何在 asp.net 中管理 XML Schema 文件,添加元素和属性

引言

在本文中,我将通过选择所有 schema 文件元素来管理 XML Schema 文件,
并将它们显示在 DropDownList 中,向根元素添加元素,并选择元素以添加
属性到所选元素

Screenshot - xsd.jpg

背景

首先,您需要了解一些关于 xml 和 xmls schema 的信息。

使用代码

**public void load_element()** 该函数加载元素并将它们显示在
DropDownList

 
public void load_element()
   {
    DropDownList_elements.Items.Clear();    
    XmlTextReader reader = new XmlTextReader(Server.MapPath("person.xsd"));
    // declare new xmlschema variable
    XmlSchema my_xmlschema = new XmlSchema();
    // read xsd file
    my_xmlschema = XmlSchema.Read(reader, null);
    reader.Close();
    // declare new xmlschema Element
    XmlSchemaElement my_xmlschemaelement = new XmlSchemaElement();
    // count how many item in XmlSchema 
    int counter = my_xmlschema.Items.Count;
    // select the root Element in the xsd file in that case "person"
    // so we loop in all element till we get "person" element
    for (int i = 0; i < counter; i++)
       {
         XmlSchemaElement current_element = (XmlSchemaElement)my_xmlschema.Items[i];
         // if the current elemet is the root element "person"
         if (current_element.Name.ToString() == "person")
           {
             // go in complextype
             XmlSchemaComplexType current_element_complexType = (XmlSchemaComplexType)current_element.SchemaType;
             // go in Sequence
             XmlSchemaSequence current_elemen_sequence = (XmlSchemaSequence)current_element_complexType.Particle;
             // loop in Sequence
               foreach ( XmlSchemaObject my_XmlSchemaObject in current_elemen_sequence.Items)
                 {
                   XmlSchemaElement my_XmlElement = (XmlSchemaElement)my_XmlSchemaObject;
                   // get the element type name
                   string[] TypeName = my_XmlElement.SchemaTypeName.ToString().Split(':');
                   // add the element name and type to DropDownList
                   DropDownList_elements.Items.Add(my_XmlElement.Name.ToString() + " " + "-" + " " + TypeName[2]);
                  
        }
            }
                 }

    DropDownList_elements.DataBind();

   }

**Button_add_element:** 该按钮从 TextBox 获取元素名称,从
下拉列表中获取类型,并将其添加到根元素,然后调用 void load_element() 函数加载
添加新元素后的元素。

 
protected void Button_add_element_Click(object sender, EventArgs e)

{

     XmlTextReader reader = new XmlTextReader(Server.MapPath("person.xsd"));
     // declare new xmlschema variable
     XmlSchema my_xmlschema = new XmlSchema();
     // read xsd file
     my_xmlschema = XmlSchema.Read(reader, null);
     reader.Close();
     // declare new xmlschema Element
     XmlSchemaElement my_xmlschemaelement = new XmlSchemaElement();
     // count how many item in XmlSchema 
     int counter = my_xmlschema.Items.Count;
     // select the root Element in the xsd file in that case "person"
     // so we loop in all element till we get "person" element
    for (int i = 0; i < counter; i++)
    {
      // save the current element in new xml variable 
      XmlSchemaElement current_element = (XmlSchemaElement)my_xmlschema.Items[i];
      // if the current elemet is the root element "person"
      if (current_element.Name.ToString() == "person")
         {
            // go in complextype
            XmlSchemaComplexType current_element_complexType = (XmlSchemaComplexType)current_element.SchemaType;
            // go in Sequence
            XmlSchemaSequence current_elemen_sequence = (XmlSchemaSequence)current_element_complexType.Particle;
            // create new element
            XmlSchemaElement new_element = new XmlSchemaElement();
            // set the nme of the new element
            new_element.Name = TextBox_element_name.Text;
            // set the type of the new element 
            new_element.SchemaTypeName = new XmlQualifiedName(DropDownList_type.SelectedValue.ToString(), "http://www.w3.org/2001/XMLSchema);">http://www.w3.org/2001/XMLSchema);
           // add the new element to our current elemen sequence
           XmlSchemaObject new_element_object = (XmlSchemaObject)new_element;
           current_elemen_sequence.Items.Add(new_element_object);
          }
      }
    // careat file stream 
    FileStream writer = new FileStream(Server.MapPath("person.xsd"), System.IO.FileMode.Create);
    // wrire the new schema 
    my_xmlschema.Write(writer);
    // close the file stream
    writer.Close();
    // load the xsd file after adding new element
    load_element();
}

  

**Button_add_attribute:** 该按钮从文本框获取元素名称,从
下拉列表中获取类型,并将属性添加到所选元素,并检查所选元素是否没有属性,则创建新的复杂类型并添加属性,如果元素
具有属性,则将元素添加到当前复杂类型中。

protected void Button_add_attribute_Click(object sender, EventArgs e)

{
     XmlTextReader reader = new XmlTextReader(Server.MapPath("person.xsd"));
     // declare new xmlschema variable
     XmlSchema my_xmlschema = new XmlSchema();
     // read xsd file
     my_xmlschema = XmlSchema.Read(reader, null);
     reader.Close();
     // declare new xmlschema Element
     XmlSchemaElement my_xmlschemaelement = new XmlSchemaElement();
     // count how many item in XmlSchema 
     int counter = my_xmlschema.Items.Count;
     // select the root Element in the xsd file in that case "person"
     // so we loop in all element till we get "person" element
    for (int i = 0; i < counter; i++)
      {
         // save the current element in new xml variable 
         XmlSchemaElement current_element = (XmlSchemaElement)my_xmlschema.Items[i];
         // if the current elemet is the root element "person"
           if (current_element.Name.ToString() == "person")
              {
                  // go in complextype
                  XmlSchemaComplexType current_element_complexType = (XmlSchemaComplexType)current_element.SchemaType;
                 // go in Sequence
                 XmlSchemaSequence current_elemen_sequence = (XmlSchemaSequence)current_element_complexType.Particle;
                 // loop in root element "person" sequence items
                 foreach (XmlSchemaObject my_XmlSchemaObject in current_elemen_sequence.Items)
                     {
                        // convert the XmlSchemaObject to XmlSchemaElement
                        // so we can access the element properties like name
                        XmlSchemaElement my_XmlElement = (XmlSchemaElement)my_XmlSchemaObject;
                        string[] ElementName = DropDownList_elements.SelectedValue.ToString().Split(' ');
                        // use if condition to get in the selected element
                            if (my_XmlElement.Name.ToString() == ElementName[0])
                                {
                                   // is thar any complex type oready in the selected element
                                       if (my_XmlElement.SchemaType == null )
                                           {
                                               // create new XmlSchemaComplexType
                                               XmlSchemaComplexType new_XmlSchemaComplexType = new XmlSchemaComplexType();
                                               // ctreat new Attribute
                                               XmlSchemaAttribute new_XmlSchemaAttribute = new XmlSchemaAttribute();
                                               // set the new Attribute name
                                                new_XmlSchemaAttribute.Name = TextBox_Attribute.Text;
                                                // set the new Attribute type
                                                new_XmlSchemaAttribute.SchemaTypeName = new XmlQualifiedName(DropDownList_Attribute.SelectedValue.ToString(), "http://www.w3.org/2001/XMLSchema);">http://www.w3.org/2001/XMLSchema);
                                                // convert new_XmlSchemaAttribute variable to XmlSchemaObject so we can add the Attribute
                                                // to the new complex type
                                                XmlSchemaObject new_XmlSchemaAttribute_object = (XmlSchemaObject)new_XmlSchemaAttribute;
                                                // add the new Attribute to the new complex type
                                                new_XmlSchemaComplexType.Attributes.Add(new_XmlSchemaAttribute_object);
                                                // add the new ComplexType to the selected element
                                                my_XmlElement.SchemaType = new_XmlSchemaComplexType;
                                              }
                                              else
                                              {
                                                  // go in selected element complextype
                                                  XmlSchemaComplexType select_elemet_XmlSchemaComplexType = (XmlSchemaComplexType)my_XmlElement.SchemaType;
                                                   // ctreat new Attribute
                                                  XmlSchemaAttribute new_XmlSchemaAttribute = new XmlSchemaAttribute();
                                                   // set the new Attribute name
                                                   new_XmlSchemaAttribute.Name = TextBox_Attribute.Text;
                                                   // set the new Attribute type
                                                   new_XmlSchemaAttribute.SchemaTypeName = new XmlQualifiedName(DropDownList_Attribute.SelectedValue.ToString(), "http://www.w3.org/2001/XMLSchema);">http://www.w3.org/2001/XMLSchema);
                                                  // convert new_XmlSchemaAttribute variable to XmlSchemaObject so we can add the Attribute
                                                 // to the new complex type
                                                 XmlSchemaObject new_XmlSchemaAttribute_object = (XmlSchemaObject)new_XmlSchemaAttribute;
                                                 // add the new Attribute to the selected element complex type
                                                  select_elemet_XmlSchemaComplexType.Attributes.Add(new_XmlSchemaAttribute_object);
                                               }
                                       }
                             }
                 }
        }

   // careat file stream 
   FileStream writer = new FileStream(Server.MapPath("person.xsd"), System.IO.FileMode.Create);
   // wrire the new schema 
   my_xmlschema.Write(writer);
   // close the file stream
   writer.Close(); 
}

 
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
           load_element();
     }
}

关注点

控制和管理 XML Schema 元素的主要思想**。**

© . All rights reserved.