Groups > Asp .Net > Data Access and ObjectDataSource Control > Dynamic Linq XML Generation - how to?




Dynamic Linq XML Generation - how to?

Dynamic Linq XML Generation - how to?
Thu, 3 Apr 2008 01:24:09 +0000
Hi Gang,

First of all, I really like how easy (and logical) it is to create XML documents
directly from my dataset.  This works great when grabbing one record, however,
mapping out my db in the easiest way it will be to create my XML document and
would like some advice.

I've got a list of, say keywords to add to a parent element.  I can store these
as a comma-delimited field, but come XML generation time, may prove problematic,
as this is a 1..* relationship to the parent, who needs a separate child element
per keyword.  The other option is to store them in another table, and add them
to the parent.

So what I'm trying to do is:

[tbl.Keyword] = "one,two,three,four,five"

<Title>Title 1</Title>
<Description>Description1</Description>
<Keywords>
     <Keyword>one</Keyword>
     <Keyword>two</Keyword>
     <Keyword>three</Keyword>
     <Keyword>four</Keyword>
     <Keyword>five</Keyword>
</Keywords>

Also, say there are no values in tbl.Keyword, then omit creating <Keywords
/>. 

I'm familiar with how to dynamically create my queries (using Dynamic.cs from
the new VS 2008 libraries but how can (or can I even use these) I apply these to
what I want to do?

How would I modify the following code to accommodate these requirements?
 

1            XElement xmlIDIF = new XElement("IDIF",
2                from tbl in db.tbl_MainCreatives
3                where tbl.CampaignID == _CampaignID
4                && tbl.PageType == "TLP"5               
orderby tbl.Title
6                select new XElement("DOC", new
XAttribute("url", tbl.FileUrl),
7    new XElement("CONTENT", new XAttribute("type",
"text/html"),
8    new XElement("html",
9    new XElement("head",
10   new XElement("title", tbl.Title),
11   new XElement("meta", new XAttribute("name",
"keywords"), new XAttribute("content", tbl.Keywords)),
12   new XElement("meta", new XAttribute("name",
"description"), new XAttribute("content", tbl.Description))
13                           ),
14   new XElement("body", tbl.Body)
15                       )
16                   ),
17   new XElement("PROP", new XAttribute("name",
tbl.TrackUrl))
18               )   
19           );


 

 Thanks.
Post Reply
about | contact