I have the following xml code;

<item>
      <title>CBS News' Logan victim of 'brutal' Egypt attack</title>
      <description>
        <![CDATA[<p><a href="http://today.msnbc.msn.com/id/41607923/ns/today-entertainment/"><img align="left" border="0" src="http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/110215-lara-logan-egypt.thumb.jpg" alt="CBS Correspondent Lara Logan is pictured in Cairo's Tahrir Square moments before she was assaulted in this photograph taken on Friday, Feb. 11." style="margin:0 5px 5px 0" /></a>The correspondent is recovering in a U.S. hospital from a "sustained sexual assault and beating" she suffered while reporting on the tumultuous events in Cairo last Friday.</p><br clear="all" />]]>
      </description>
      <link>http://today.msnbc.msn.com/id/41607923/ns/today-entertainment/</link>
      <media:content medium="image" url="http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/110215-lara-logan-egypt.thumb.jpg">
        <media:credit role="provider">Reuters</media:credit>
        <media:text>
          <![CDATA[<p><a href="http://today.msnbc.msn.com/id/41607923/ns/today-entertainment/"><img align="left" border="0" src="http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/110215-lara-logan-egypt.thumb.jpg" alt="CBS Correspondent Lara Logan is pictured in Cairo's Tahrir Square moments before she was assaulted in this photograph taken on Friday, Feb. 11." style="margin:0 5px 5px 0" /></a></p><br clear="all" />]]>
        </media:text>
      </media:content>
      <pubDate>Tue, 15 Feb 2011 23:41:31 GMT</pubDate>
      <category>News</category>
      <guid isPermaLink="true">http://today.msnbc.msn.com/id/41607923/ns/today-entertainment/</guid>
    </item>

I need to extract the single sentence "BS Correspondent Lara Logan is pictured in Cairo's Tahrir Square moments before she was assaulted in this photograph taken on Friday, Feb. 11." from the code. Can anyone tell me the best way to do this. Thanks in advance.

What exactly would you like to do?
BTW: there are two sentances in the upper xml code. Which one?

What exactly would you like to do?
BTW: there are two sentances in the upper xml code. Which one?

I want to get the description text from each item and put them in to an array. The RSS feed is a news site, and the idea is I get the title of the news item and the description, so I can display them.

So you want to get these:

<title>CBS News' Logan victim of 'brutal' Egypt attack</title>
      <description>
        <![CDATA[<p><a href="http://today.msnbc.msn.com/id/41607923/ns/today-entertainment/"><img align="left" border="0" src="http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/110215-lara-logan-egypt.thumb.jpg" alt="CBS Correspondent Lara Logan is pictured in Cairo's Tahrir Square moments before she was assaulted in this photograph taken on Friday, Feb. 11." style="margin:0 5px 5px 0" /></a>The correspondent is recovering in a U.S. hospital from a "sustained sexual assault and beating" she suffered while reporting on the tumultuous events in Cairo last Friday.</p><br clear="all" />]]>
      </description>

For the title what is between <title> and </title>
for the description what is between <description> and </description>

Solution: set flags of these marks (<title>, </title>) and use IndexOf method on 1st mark and IndexOf to tget the 2nd mark (beginning and th end of) and then use these two indexes to get the text between those indexes.

If you need any help, let me know, ok?
bye,
Mitja

Ok, I have chosen to use a Dictonary, which is a pretty good idea, becuase this object can store keys and their values. So in your case as a key you have "title" and values is "some text".

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Feb16ReadFile_GetText
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Method();
        }

        private void Method()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("title", String.Empty);
            dic.Add("description", String.Empty);

            string path = @"C:\1\test14.xml";
            using (XmlTextReader reader = new XmlTextReader(path))
            {
                try
                {
                    string _key = null;
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                {
                                    _key = reader.Name;
                                    break;
                                }
                            case XmlNodeType.Text:
                                {
                                    if (_key == "title" || _key == "description")
                                    {
                                        dic[_key] = reader.Value;
                                    }
                                    break;
                                }
                            case XmlNodeType.CDATA:
                                {
                                    if (_key == "title" || _key == "description")
                                    {
                                        string tagS = "alt=\"";
                                        string tagE = "\"";
                                        int indexS = reader.Value.IndexOf(tagS) + tagS.Length;
                                        int indexE = reader.Value.IndexOf(tagE, indexS) - tagE.Length;
                                        string myText = reader.Value.Substring(indexS, indexE - indexS);
                                        dic[_key] = myText;
                                    }
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }
                }
                catch { }
            }

            //to show whats is in Directory you do:
            string textToShow = null;
            foreach (KeyValuePair<string, string> item in dic)
            {
                string key = item.Key;
                string value = item.Value;

                //to put all into one string to show later in messageBox:
                textToShow += key.ToUpper() + ": " + value + Environment.NewLine;
            }
            MessageBox.Show("These is the outcome:\n\n" + textToShow);
        }
    }
}

Hope it helps,
Mitja

Forgot to tell you how to retreive dara from Dictonary object:

//to show whats is in Directory you do:
            foreach (KeyValuePair<string, string> item in dic)
            {
                string key = item.Key;
                string value = item.Value;
            }

Now you have all you need. I hope you are happy with the code.

PS: The path in the upper post is just my example path. You have to set it your own to the xml file!

Mitja

Thanks that helped.

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.