Member Avatar for R3ap3R

Hi there!

I am busy creating a desktop application with Windows.Forms to display Twitter XML data inside a textbox on a form.
The XML structure looks like this:

<statusses>
	<status>
		<text>...</text>
		<user>
			<name>...</name>
		</user>
	</status>
	<status>
		<text>...</text>
		<user>
			<name>...</name>
		</user>
	</status>
</statusses>

The problem is that with my code I can get anything inside the "status" subcategory but not ALSO inside the "user" subcategory (within it). So I have two separate calls to the different subcategories.

Now I would like to know how can I join the two so that I can display a username and tweet with a foreach loop? Because if I use two foreach loops the first displays all usernames and then the second all tweets. I would like to display username+tweet, username+tweet.

XDocument feedXML = XDocument.Load("http://twitter.com/statuses/public_timeline.xml");
                
                var feeds = from feed in feedXML.Descendants("status")
                            select new
                            {
                                Tweet = feed.Element("text"),
                            };

                var feedz = from feed in feedXML.Descendants("user")
                            select new
                            {
                                User = feed.Element("screen_name"),
                            };

                // This does not work
                var query = feedz.Concat(feeds);



                foreach (var group in query)
                {
                    textUpdates.AppendText(group.User.Value.ToString() + "\r\n" + group.Tweet.Value.ToString());
                }

Otherwise this works but it displays username+username+username followed by tweet+tweet+tweet

XDocument feedXML = XDocument.Load("http://twitter.com/statuses/public_timeline.xml");
                
                var feeds = from feed in feedXML.Descendants("status")
                            select new
                            {
                                Tweet = feed.Element("text"),
                            };

                var feedz = from feed in feedXML.Descendants("user")
                            select new
                            {
                                User = feed.Element("screen_name"),
                            };


                foreach (var feed in feeds)
                    {
                        textUpdates.AppendText(feed.Tweet.Value.ToString() + "\r\n");
                    }

                    foreach (var fed in feedz)
                    {
                        textUpdates.AppendText(fed.User.Value.ToString() + "\r\n");
                    }

Any solution? :P

Maybe you can use a for loop instead of a foreach like:

XDocument feedXML = XDocument.Load("http://twitter.com/statuses/public_timeline.xml");
                
                var feeds = from feed in feedXML.Descendants("status")
                            select new
                            {
                                Tweet = feed.Element("text"),
                            };

                var feedz = from feed in feedXML.Descendants("user")
                            select new
                            {
                                User = feed.Element("screen_name"),
                            };


                for (int i=0;i<feeds.Count;i++)
                    {
                        textUpdates.AppendText(feeds[i].Tweet.Value.ToString() + "\r\n");

                        textUpdates.AppendText(feedz[i].User.Value.ToString() + "\r\n");
                    }

Hope this helps

Member Avatar for R3ap3R

Thank you lolafuertes, but unfortunately that does not work. The error I get is "Operator ‘<’ cannot be applied to operand of type ‘int’ and ‘method group’" with:

for (int i = 0; i < feeds.Count; i++)
                {
                    textUpdates.AppendText(feeds[i].Tweet.Value.ToString() + "\r\n");

                    textUpdates.AppendText(feedz[i].User.Value.ToString() + "\r\n");
                }
Member Avatar for R3ap3R

Hi there, I got it to work in a way. It does now display username+tweet, username+tweet - but it is the same username+tweet copied throughout (10x) :(

for (int i = 0; i <= 10; i++)
                {

                    foreach (var feed in feedz)
                    {

                        textUpdates.AppendText(feed.User.Value.ToString() + ":" + "\r\n");
                        break;
                    }

                    foreach (var feed in feeds)
                    {
                        textUpdates.AppendText(feed.Tweet.Value.ToString() + "\r\n" + "\r\n");
                        break;
                    }

                }

Any suggestions?

I am unsure about the type of your feeds and feedz.
If this is an array, list or collection, there should be a Count or Length that defines how many selected descendants you got on each.

You can obtain the right property debuging the feeds. Use this to cicle over.

Comments to your code: the foreach structure will cicle over all existing elements in an array, list or collection, starting at the first. If you put a break after doing the first assignement, the next time you start the foreach, will start over the first element again, and this will produce the duplicates.

Hope this helps

Member Avatar for R3ap3R

Thank you it worked. I made the array with a counter which solved it :)

Glad to hear that :)
If this solved the problem, please be so kund to mark the thread as solved.

Thanks in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.