( ).
" " 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);
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>
</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>
</menuxml>