How to convert hierarchical database data to XML using ASP.net 3.5 and LINQ

I have a table with a hierarchical structure. like this: (source: aspalliance.com )
alt text

and table data is shown here: (source: aspalliance.com )
alt text


.

ASP.net 3.5 SP1 LINQ MSSQL Server2005. XML? Dataset Object .GetXML(). LINQtoSQL LINQtoXML??? ? ? ? , .net 3.5 featuers.


, . Dunamic SiteMap . , SiteMap ASP.net :

<siteMapNode url="~/Category?cid=0" title="Home"  description="Home">

    <siteMapNode url="~/Category?cid=1" title="a"  description="" />

    <siteMapNode url="~/Category?cid=2" title="b"  description="" >

        <siteMapNode url="~/Category?cid=3" title="c"  description="" />

        <siteMapNode url="~/Category?cid=4" title="d"  description="" />

    </siteMapNode>
</siteMapNode>

, Mr.Murph . SiteMap , . Mr.Murph ?

+3
2

( ).

" " XML Linq to SQL Linq to XML ( .NET ASP.NET) , , , ( , ):

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)))); 
}

,

  • Linq to SQL, DataContext, , , parent/child ChildMenus .
  • , , menuxml
  • linq to SQL, , , GetMenuXML XML ( ) .
  • GetMenuXML , , , lamba - (, !), -

from m in menu.ChildMenus select GetMenuXML(m)

(!), XML -

<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>
+2

, . , . , , , GetMenuXML() . :

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))));
} 
+2

Source: https://habr.com/ru/post/1745228/


All Articles