954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

XML Request Problem

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

R3ap3R
Newbie Poster
11 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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

lolafuertes
Master Poster
793 posts since Oct 2008
Reputation Points: 120
Solved Threads: 166
 

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");
                }
R3ap3R
Newbie Poster
11 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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?

R3ap3R
Newbie Poster
11 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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

lolafuertes
Master Poster
793 posts since Oct 2008
Reputation Points: 120
Solved Threads: 166
 

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

R3ap3R
Newbie Poster
11 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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

Thanks in advance

lolafuertes
Master Poster
793 posts since Oct 2008
Reputation Points: 120
Solved Threads: 166
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You