SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Menu from XML file

rated by 0 users
This post has 2 Replies | 1 Follower

Top 50 Contributor
Posts 41
Radhiga Hmm [^o)] Posted: 05-19-2005 2:33 PM

Hello Friends,

                          Im a .net developer.but i havent used xml extensively. now i heard one requirement. say i have to build a menu, using database fields..For that idea behind that logic is, need to create a xml file with parent menu and child level manu..Maxmimun 4 child level be there. i have writeen the query to get the result..now my question is how can i stroe that in a xml file..

say it should be like this

<parent>Main Menu</parent>

      <child>SubOne</child>

      <child>SubOne1</child>

      <child>SubOne2</child>

somethink like this...i know to use the xmlreader..but i dont know how to store this in XML file to do the further functionality in that.

pls guide me

 

 

Top 25 Contributor
Posts 232

The sample code below actually produce the result as indicated by you. It output to the console, but you can easily save it to an xml file. Since I don't know what's actually stored in your returned query, you just have to write a loop similar like this to make it work for you:

// You know some code like this
while (Reader.Read())
{
    if (Reader.GetString(index) == "Parent")
  {
     Writer.WriteStartElement("Parent");
     Writer.WriteString("Main Menu");       
  }
}
// You know some code like this

MenuWriter.cs
using System;
using System.IO;
using System.Xml;

namespace Test
{
  public class MenuWriter
  {
    public static void GenerateMenu()
    {
      XmlTextWriter Writer = new XmlTextWriter(Console.Out);
      try
      {
        Writer.Formatting = Formatting.Indented;
 Writer.Indentation = 2;
       
        //Write the beginning of the document including the
        //document declaration. Standalone is true.
        Writer.WriteStartDocument(true);
       
          Writer.WriteStartElement("Parent");
          Writer.WriteString("Main Menu");
                   
            Writer.WriteStartElement("Child1");
            Writer.WriteString("SubOne");
           
              Writer.WriteStartElement("Child2");
              Writer.WriteString("SubOne2");
           
                Writer.WriteElementString("Child3", "SubOne3");
             
              Writer.WriteEndElement();
         
            Writer.WriteEndElement();
                         
          Writer.WriteEndElement();     
     
        Writer.WriteEndDocument();
      }
      finally
      {
        Writer.Close();
      }
    }
  }
 
  public class Test
  {
    public static void Main(string[] args)
    {
      MenuWriter.GenerateMenu();
    }
  }       
}

Type this at the command prompt: csc MenuWriter.cs

Hope it helps

Top 25 Contributor
Posts 485
 Radhiga wrote:

..Maxmimun 4 child level be there. i have writeen the query to get the result..now my question is how can i stroe that in a xml file..



Hi,

basically you can save it by using the XmlDocument object too once you parse it into a xml string

example your file is filename.xml

so

//C#
using System.XML

XmlDocument D= new XmlDocument();
D.Load("filename.xml"); //if the source xml is a file

//OR

D.LoadXml(somexmlstring); //if the source xml is a string
          
//Do some changes, example:
D.FirstChild.Value = "Hello World";

//Save the changes back to same file
D.Save("filename.xml");

Big Smile [:D]
Yes, another version, another round of headaches. But that's progress.
Page 1 of 1 (3 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems