views:

195

answers:

3

I've been feeling like start doing some fun stuff with my delicious bookmarks and LINQ to XML, and I was wondering if there's a way to split the tag attribute within LINQ.

What I've meant with split the tag within LINQ was generating a collection of strings for each post element, so the expected result would be a generic collection of post elements, with its attributes as properties, where the tag property is itself another collection of strings, with its items being each tag.

For those not quite familiar with delicious.com's exported xml, here´s the basic structure of an element:

<post
    href="http://stackoverflow.com/"
    hash="e4a42d992025b928a586b8bdc36ad38d"
    description="Stack Overflow"
    tag="code development programming community tips answers reference"
    time="2009-05-22T19:44:09Z"
    extended="Stack Overflow is a programming Q & A site that's free."
    meta="e0bf85c9df073cd51cc5528637db5277"
 />

Here's the snippet of code I'm using:

   XDocument delicious = XDocument.Load("all.xml");

  var bookmarks = from posts in delicious.Descendants().Attributes("tag")
                  select (string)posts;

Any ideas, suggestions, comments would be really appreciated.

+1  A: 

string class has a Split method.
Is this what you are looking for?

EDIT: string.Join(Environment.NewLine, bookmarks.ToArray());

shahkalpesh
Not really, actually I need to split them *within* LINQ so I can expose them to its operators such as count() and sum()
Nano Taboada
+1  A: 

What is your desired output actually like? Do you want an array of each value in the "tag" field?

var bookmarks = from posts in delicious.Descendants().Attributes("tag")
                    select ((string)posts).Split(" ");

That would give you a list of arrays. It's hard to say how to accomplish what you want, since it isn't very well specified.

Andrew Backer
Thanks for your reply Andrew, actually I want to be able to compute those tags like if they were independent, say, how many "development" tags I've added in a particular month, et cétera.
Nano Taboada
You could get the string and add a where clause with Contains " development ". That will get you posts with attributes having development as part of its tag.
shahkalpesh
Thank you shahkalpesh! I've been thinking about this approach which at the moment is probably the best possible solution.
Nano Taboada
+1  A: 

What I did was to use 'set' and assign the value to that variable, and then used set to continue to manipulate, so at the end I was able to just use it in my linq query, as it was in a form that was now useful to me. :)

James Black
Thanks for your contribution James! mind elaborating a bit more?
Nano Taboada
Sorry about taking so long to respond, but here is something I did: var q = from file in Directory.GetFiles(...) let fi = new FileInfo(file) let c = XElement.Load(file) as XElement let livetime = !string.IsNullOrEmpty(c.Element("LiveTime").Value) ? int.Parse(c.Element("LiveTime").Value) : -1 select new { DeadTime = deadtime,By using set you can do manipulations on the attribute.
James Black