views:

25

answers:

1

hi. i want to generate XML for the following tree type structure. i attached picture. generated xml should be

 <services>
  <service>
    <name>Service 1</name>
    <categories>
      <category>
        <name>Cateogry 1</name>
        <methods>
          <method>
            <name>Method 1</name>
          </method>
        </methods>
      </category>
    </categories>
  </service>
  <service>
    <name>Service 2</name>
    <categories>
      <category>
        <name>Cateogry 1</name>
        <methods>
          <method>
            <name>Method 1</name>
          </method>
        </methods>
      </category>
    </categories>
  </service>
  <service>
    <name>Service 3</name>
    <categories>
      <category>
        <name>Cateogry 1</name>
        <methods>
          <method>
            <name>Method 1</name>
          </method>
        </methods>
      </category>
    </categories>
  </service>
</services>

Tree Image

A: 

I solved my problem:)

XElement elServices = new XElement("services");            

            foreach (TreeNode serviceNode in treeView1.Nodes)
            {
                XElement elService = new XElement("service",
                    new XElement("name", serviceNode.Text));

                XElement elCategories = new XElement("categories");

                foreach (TreeNode categoryNode in serviceNode.Nodes)
                {                    
                    XElement elCategory = new XElement("category",
                    new XElement("name", categoryNode.Text));

                    XElement elMethods = new XElement("methods");

                    foreach (TreeNode methodNode in categoryNode.Nodes)
                    {
                        XElement elMethod = new XElement("method",
                                            new XElement("name", methodNode.Text));

                        elMethods.Add(elMethod);
                    }
                    elCategory.Add(elMethods);
                    elCategories.Add(elCategory);
                }
                elService.Add(elCategories);
                elServices.Add(elService);                
            }           

            MessageBox.Show(elServices.ToString());
Mohsan