如何使用ASP.net 3.5和LINQ数据库分层数据转换为XML [英] how to convert Database Hierarchical Data to XML using ASP.net 3.5 and LINQ

查看:100
本文介绍了如何使用ASP.net 3.5和LINQ数据库分层数据转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有层次结构的表。像这样:


i have a table with hierarchical structure. like this:





这种策略让我有无限的类别和子类别的能力。


我使用ASP.net 3.5 SP1和LINQ和MSSQL Server2005中。 如何将它转换为XML?我可以做到这一点的 DataSet对象的和 .GetXML()的方法。但如何与LINQtoSQL或LINQtoXML实现它?或者,如果有另一个来执行更简单的方法?你有什么建议吗?最好的办法?我在网上搜索,但一无所获的.NET 3.5 featuers。



问题更新

thnks到墨菲,但现在我有一个新的问题。我想打一个Dunamic sitemap文件在我的项目。你知道在ASP.net站点地图文件看起来像这样:

and table data shown here:

this strategy gives me the ability to have unbounded categories and sub-categories.

i use ASP.net 3.5 SP1 and LINQ and MSSQL Server2005. How to convert it to XML? I can Do it with Dataset Object and ".GetXML()" method. but how to implement it with LINQtoSQL or LINQtoXML??? or if there is another simpler way to perform that? what is your suggestion? the best way? I searched the web but found nothing for .net 3.5 featuers.

Question Update
thnks to Murph but Now I have a New Problem. I want to Make a Dunamic SiteMap file in my Project. You Know the SiteMap file in ASP.net looks like this:

<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的code子类别将嵌套元素。但在网站地图情况下,我们没有这样的一个元素的所有类别和子类别和嵌套中的元素。我怎样才能改变Mr.Murph code来塑造这个模式?

Main Problem is that according to Mr.Murph's Code the sub categories will nested in element. but in SiteMap Case we have no such a elements and all of categories and sub categories are nested in elements. how can I change Mr.Murph code to shape this Schema?

推荐答案

好吧非常迅速(彻底未经测试)。

Ok very quickly (and thoroughly untested).

要滚你自己的使用LINQ to SQL和LINQ to XML(这是通用的.NET ASP.NET不特定的)是相当简单的XML,这应该给予一些假设我的细节,之后会(现在是足够按照建议)略作修改:

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

好吧,从顶部


  • 我假设叫的DataContext一个LINQ到SQL数据上下文中,你必须映射到菜单和父/子关系是ChildMenus在父表底。

  • 我们创建一个新的文件

  • 我们创造,我们将作为根使用新的元素,我们会打电话给menuxml

  • 我们然后做一个LINQ to SQL查询,选择所有没有父母,并呼吁GetMenuXML输出​​为一个菜单记录XML(即记录及其子女)这些条目菜单项。

  • GetMenuXML输出​​一个菜单项分裂中间的属性和元素的唯一略有不同的是,我已经使用了兰巴前pression,而不是详细的语法生成子菜单 - 但如果我知道了右(还记得没有测试!)这就是做线沿线的东西

从menu.ChildMenus米选择GetMenuXML(M)

如果它的工作原理(!),你应该得到的XML类似

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>

这篇关于如何使用ASP.net 3.5和LINQ数据库分层数据转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆