Click here to Skip to main content
11,708,974 members (72,689 online)
Click here to Skip to main content

Manage XML Schema File ( add element - Attribute)

, 8 Aug 2007 23.4K 230 22
Rate this:
Please Sign up or sign in to vote.
How to manage XML Schema file add element and attribute in asp.net

Introduction

in that Article i will manage XML Schema File by selecting all the schema file elements and
display them in DropDownList , Adding Elements to root element and select elemet to add an
attributes to the selected element

Screenshot - xsd.jpg

Background

First you must know some information abut xml and xmls schema .

Using the code

public void load_element()that funcation load the element and display them in
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 : that button get the element name from TextBox and type from
drodownlist and add it in root element then call the void load_element() funcation to load the
element aftar adding the new 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(), <a href="http://www.w3.org/2001/XMLSchema);">http://www.w3.org/2001/XMLSchema</a>);
           // 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 : that button get element name from textbox and type from
drodownlist and add the attribute to the selected element and cheak if the selected element dont have attribute creat new complex type and add the attribute in and if the element have
an attribut add the element in the current complex type.

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(), <a href="http://www.w3.org/2001/XMLSchema);">http://www.w3.org/2001/XMLSchema</a>);
                                                // 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(), <a href="http://www.w3.org/2001/XMLSchema);">http://www.w3.org/2001/XMLSchema</a>);
                                                  // 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();
     }
}

Points of Interest

main idea of controling and manageing XML Schema Elements.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

mohamed antar
Web Developer
Egypt Egypt
No Biography provided

You may also be interested in...

Comments and Discussions

 
Generalerror processing xsd file Pin
madhanprabu4-Feb-09 2:23
membermadhanprabu4-Feb-09 2:23 
GeneralProblem in code ......please help Pin
hemendravyas16-Aug-07 20:51
memberhemendravyas16-Aug-07 20:51 
GeneralRe: Problem in code ......please help Pin
mohamed antar17-Aug-07 23:48
membermohamed antar17-Aug-07 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 8 Aug 2007
Article Copyright 2007 by mohamed antar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid