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
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 thiswhile (Reader.Read()){ if (Reader.GetString(index) == "Parent") { Writer.WriteStartElement("Parent"); Writer.WriteString("Main Menu"); }}// You know some code like thisMenuWriter.csusing 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
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..
..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..