score:2

Accepted answer

ok very quickly (and thoroughly untested).

to "roll your own" xml using linq to sql and linq to xml (which are generic .net not asp.net specific) is fairly simple, this should be sufficient given a few assumptions which i'll detail after (now modified slightly as suggested):

void main()
{
    datacontext dc = new datacontext();

    menuxml = new xdocument();
    xelement root = new xelement("menuxml",
        from m in dc.menus
        where m.parentid == null
        select getmenuxml(m));

    menuxml.add(root);
    // you've now got an xml document that you can do with as you need
    // for test you can save...
    menuxml.save("filename.xml");
}

private static xelement getmenuxml(menu menu) 
{ 
    return new xelement("category",  
        new xattribute("menuid", menu.menuid), 
        new xattribute("text", menu.text), 
        new xelement("description", menu.description), 
        new xelement("menus", menu.menus.select(m => getmenuxml(m)))); 
}

ok, from the top

  • i'm assuming a linq to sql data context called datacontext in which you've got the table mapped to menus and that the parent/child relation is childmenus at the parent end.
  • we create a new document
  • we create a new element that we'll use as the root that we'll call menuxml
  • we then do a linq to sql query to select all the menu entries that don't have a parent and call getmenuxml to output the xml for a single menu record (i.e that record and its children) for those entries.
  • getmenuxml outputs a single menu entry split betwixt attributes and elements the only slightly different thing is that i've used a lamba expression instead of the verbose syntax for generating the child menus - but if i've got it right (remember no test!) thats doing something along the lines of

from m in menu.childmenus select getmenuxml(m)

if it works (!) you should get xml something like

<menuxml>
    <menu menuid="1" text="product">
        <description>a list of products</description>
        <menus>
            <menu menuid="6" text="background">
                <description>product background</description>
                <menus>
                    <menu menuid="18" text="internet restriction">
                        <description>internet restriction</description>
                        <!-- children if any would be here -->
                    </menu>
                    <menu menuid="19" text="speed solution">
                        <description>speed solutions</description>
                    </menu>
                </menus>
            </menu>
            <menu menuid="7" text="background">
                <description>product details</description>
            </menu>
        </menus>
    </menu>
    <!-- rest of the top level menus -->
</menuxml>

score:2

thank you dear murph. i used your code and it worked well. but as you say, you didn't test your code so getmenuxml() had some mistakes. here is the correction:

private static xelement getmenuxml(menu menu)
{
    return new xelement("category", 
        new xattribute("menuid", menu.menuid),
        new xattribute("text", menu.text),
        new xelement("description", menu.description),
        new xelement("menus", menu.menus.select(m => getmenuxml(m))));
} 

Related Query

More Query from same tag