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
- Linq to SQL with GroupBy and Detail - Is there a better way to do this?
- display it into the "Table1" table
- Convert two arrays to a new one using LINQ
- Issues using result from LINQ query, in other LINQ query
- LINQ to SQL query using "NOT IN"
- Unable to use certain functions with LINQ to Entities?
- Linq cannot select Expression<Action>?
- Postgres - Geospatial Search in LINQ or SQL with PostGis
- How do I get first element of each array via Linq C#
- Linq Select * from Table ExecuteQuery
- What is the best way to remove all instances from a Dictionary?
- Reading multiple files with LINQ
- EF - How to edit the below model using linq?
- How to filter List with multiple 'and' 'or' conditions
- Creating Nested Groupings using Linq Extension Methods
- grouping and Update Property in linq
- Map List by Linq Expression
- c# linq to xml error: cannot implicitly convert type
- Linq group by multiple columns with anonymous column
- Remove duplciates from a textbox with .Distinct();
- Three trier Linq query
- Merge nested objects in one list using Linq
- How to get a list of matched ID's using Linq?
- Elegant way to transform arrays in C#?
- How should I add where condition in lambda expression
- how to update a field table by linq in C#?
- Compare input double with datatable string column values within a range using between query(SQL query to Linq on datatable c# )
- Use LINQ to populate controls from object array
- In Entity Framework, how to get the sql translated by EF?
- Linq count of records in a list