XML returning incorrect values

Asked

Viewed 261 times

3

I’m trying to do a show on to test the codes needed to run a , read your data and enter it in a list. But for this, I preferred to use a MessageBox to do the debug first.

Man is a RSS Feed that I bass directly from my Tumblr pro user device (in this case, a computer). I am using the XmlDocument to open the file and use the GetElementsByTagName to take the values.

As a gambit, I end up taking Tumblr’s title iPoema and its description Residence of works of amateur poets. There are online readers, because they use the same tags within item. I’d also like to know if you can do XmlNodeList get the tags that are inside the Child item.

EDIT

I forgot to mention that I’m having trouble getting back on the part of category of my , because he returns, even if I start with i = 0, as if the initial value were -1 and when arriving in 20 it becomes empty.

EDIT 2 Solved.

frmHome.Cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Xml;

namespace iPoema_CSharp
{
    public partial class frmHome : Form
    {
        string FULLPATH, tipo;
        string tempFolder = System.Environment.GetEnvironmentVariable("TEMP") + @"\";
        string[] titulo = new string[21];
        string[] texto = new string[21];
        string[] autor = new string[25];
        public frmHome()
        {
            InitializeComponent();
        }

        private void download(string url, string filename)
        {
            filename = tempFolder + filename;

            // Apaga o arquivo antigo
            File.Delete(filename);

            // Baixa o arquivo
            using (WebClient webClient = new WebClient())
            {
                webClient.DownloadFile(url, filename);
            }
            FULLPATH = filename;
        }

        private void readXML(string path)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            // Title
            XmlNodeList titleList = doc.GetElementsByTagName("title");
            for (int i = 0; i < 20; i++)
            {
                if (i == 0)
                {

                }
                else
                {
                    titulo[i] = titleList[i].InnerText;
                }
            }

            // Description
            XmlNodeList descList = doc.GetElementsByTagName("description");
            for (int i = 0; i < 20; i++)
            {
                if (i == 0)
                {

                }
                else
                {
                    texto[i] = descList[i].InnerText;
                }
            }

            // Category
            XmlNodeList catList = doc.GetElementsByTagName("category");
            for (int i = 0; i < 19; i++)
            {
                autor[i] = catList[i].InnerText;
            }
        }

        private void bAtualizar_Click(object sender, EventArgs e)
        {
            download("http://www.ipoema.tumblr.com/rss", "iPoema.xml");
            readXML(tempFolder + "iPoema.xml");

            string text;

            for (int x = 1; x <= 19; x++)
            {
                text = texto[x];
                text = text.Replace("</p>", "\n");
                text = text.Replace("<p>", "");
                text = text.Replace("<br/>", "\n");
                text = text.Replace("&#8217;", "'");
                text = text.Replace("&#8220;", "\"");
                text = text.Replace("&#8221;", "\"");
                MessageBox.Show(text + "\n\n#" + autor[x], titulo[x], MessageBoxButtons.OK);
            }
        }
    }
}

Link: iPoema RSS

<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
    <channel>
        <description>
            Residência de obras de poetas amadores.Há leitores online
        </description>
        <title>iPoema</title>
        <generator>Tumblr (3.0; @ipoema)</generator>
        <link>http://ipoema.tumblr.com/</link>
        <item>
            <title>Mente absurdamente louca</title>
            <description>
            <p>Este sou eu,<br/>todo fodido que nem eu não há,<br/>olha pro céu e pro mar,<br/>muitos dizem que me entendem,<br/>mas será que eles vivem como a gente?</p> <p>Poderia até ser uma vida legal,<br/>mas será que o amor pode ser natural?<br/>Só penso nisso todos os dias,<br/>sei que não sou as mil maravilhas,<br/>mas pense que, em um dia,<br/>já obteve vontade de nós termos uma vida, querida?</p> <p>Nascido para morrer sozinho, nunca,<br/>mas também não morrerei sofrendo nesta espelunca,<br/>toda suja de sentimentos vazios,<br/>todos nós estamos perto do rio<br/>das lamentações, onde não há multidões<br/>querendo lhe julgar por não saber ainda o que é namorar.</p> <p>Me sinto alienado nesse sentido abstrato,<br/>muito errado esses loucos controlados,<br/>mídia filho da ****,<br/>controlando os amigos nessa vida estúpida,<br/>toda inculta, vivendo sem conduta,<br/>tomando multa, na sua fuça retardada,<br/>momentos loucos com sua vida idolatrada,<br/>que nem momentos como estes serão lembrados,<br/>nem honrados pelos seres humanos movidos pelo pouco que lhes é dado.</p>
            </description>
            <link>http://ipoema.tumblr.com/post/80971539116</link>
            <guid>http://ipoema.tumblr.com/post/80971539116</guid>
            <pubDate>Fri, 28 Mar 2014 10:13:37 -0300</pubDate>
            <category>Alexandre de Souza</category>
        </item>
        [...]
    </channel>
</rss>

1 answer

3


The selection by label name (tag name) is always dangerous because one can fall in cases like this where elements with the same label name have different meaning.

In this case /rss/channel/description does not have the same meaning of /rss/channel/item/description.

To perform a more precise query you will need to use Xpath.

To get the descriptions of the items try something like this:

var xml = new XmlDocument();
xml.Load("http://www.ipoema.tumblr.com/rss");
foreach(XmlElement node in xml.SelectNodes("/rss/channel/item/description"))
{
    // processa o nó
}

But given that this is a known type of XML document, I support the recommendation of mdisibio for the use of the class Syndicationfeed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.