score:2
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))));
}
Source: stackoverflow.com
Related Query
- how to convert Database Hierarchical Data to XML using ASP.net 3.5 and LINQ
- How to show 2nd row on Data in Next Button Click in C# and XML database using LINQ
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Using LINQ to iterate through a database and generate a generic XML layout of schema + data
- I am using LINQ to connect to a database and i get a list of properties and I want to convert it to an XML File
- how to insert data in 1 table and update another table in a sql database using linq
- How do you populate a parent child data structure from XML where generic List<> objects are in parent and child structures using one LINQ query?
- How do I get SOAP xml file data using LINQ and display it in a form's text box? C#
- How much is too much data for and XML file, and what are some file based database alternatives?
- How can I code an outer join using LINQ and EF6?
- How to Insert a New Node Using LINQ to XML when only xml data is available?
- Is a full list returned first and then filtered when using linq to sql to filter data from a database or just the filtered list?
- How to parse XML header and nested sections using LINQ
- can't save data to database using ajax and linq
- How can I group and sum XML data with the following structure using LINQ?
- How to flatten a multi level XML into a single level XML using c# code LINQ
- How to implement generic method approach using Linq and XML
- Dynamic Linq using Data Objects. How to convert Int32 to String for purpose of calling String.Contains()
- How to filter related data using Entity Framework and LINQ to SQL and LinqKit PredicateBuilder Or IdeaBlade DevForce
- How to group table data and sum by multiple columns using LINQ or another approach
- how take data from xml using linq to xml
- LINQ - database query using "into", how do I reference data prior to "into" in the select clause?
- How to dynamically query the database using LINQ and a variable?
- Why did I get an exception "Cannot implicitly convert type 'bool' to 'long?'" when using the LINQ Sum method and how to fix it?
- how do i convert Dictionary string string to array of key value pair of string and string using linq and c#
- How to read data from xml file using linq to xml?
- Using LINQ and EF, how to remove values from database where not in list of items
- How do I group items from a collection using LINQ and return the shaped data according the collection type
- Linq ebay XML Parsing ASP and C# using a query style?
More Query from same tag
- How do I query multiple lists as well as select multiple items
- Can I filter children rather than nested for loop using LINQ
- Using LINQ and EF, how to remove values from database where not in list of items
- Changing variable in LINQ select
- Null reference in LINQ query
- Querying a List of Objects
- How to convert linq expression to dictionary<string,string>
- Issue to compose a nested relation query with Entity Framework
- How to update a table row, given the RowID and the Value to update. MVC C# Linq
- QueryOver with constraint on one of the elements which is a list
- FirstOrDefault(), SingleOrDefault(), Any(), etc... Which One Is The Fastest?
- Take() Throws Not Implemented Exception Entity Framework 6 with MySQL
- Finding the most specific matching item
- LINQ left join with group by and correct count
- Find similar product using linq
- Using Expression Class With LINQ Queries
- getting the elements of a Collections.Generic through refllection
- Get field from class using string
- Linq query needs to select 3 instead of 1
- Using await and still getting a dbcontext has been disposed error
- LINQ .ToList() fails on single result
- LINQ Union not returning wanted results
- How to union two tables to override empty values and order by timestamp?
- Linq Query with Any() using in memory object and db
- Updating an Entity Without Saving the Data back to the Database
- JIT error with LINQ OrderBy using C# on iOS
- Cast System.Data.Linq.Table<T> to custom class
- Compare two collections optimization
- Get the line number of last occurrence of a particular string with [Linq.Enumerable] in PowerShell
- Hospital Stay: I cannot find a reason why my CalcTotalChargs is not being recognized