How can I split an attribute's value within LINQ(-to-XML)?
-
23-08-2019 - |
Question
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.
Solution
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.
OTHER TIPS
string class has a Split method.
Is this what you are looking for?
EDIT: string.Join(Environment.NewLine, bookmarks.ToArray());
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. :)