将XML数据解析为RichTextBox但结果中只显示一个项目

时间:2012-08-08 02:30:46

标签: c# winforms xml-parsing

我正在处理我的项目,我对C#完全不熟悉,我需要使用C#来完成我的工作。我真的希望你们能帮我解决下面的问题。非常感谢您的帮助!!感谢。

我有一个XML文件,在父<item>...</item>中有两个<channel>。我执行了我的代码,我得到的只是如下 - 一个<item>的数据出现了:

  

标题:回复:我对Tesco的看法

     解释:当它进入市场时,要清除iii IPO。   3个季度是倍数,三倍,四极,W-T-F。就像ebay一样   在那里很多竞标,也是假的。

     

今天的想法:特别是Deloitte似乎有   逃过头条新闻。与渣打银行勾结的指控   监管报告cd是安然时刻记住那些老会计师   德勤,海曼先生(RBS)?通过Hardcore Uproar

     

日期:2012年8月7日星期二14:03:00 GMT

     

作者:Hardcore Uproar

我的代码如下:

        private void btnComSearch_Click(object sender, EventArgs e)
        {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load("tsco.xml");

        XmlElement root = xmlDoc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("item"); 
        foreach (XmlNode node in nodes)
        {
            XmlNodeList comTitle = xmlDoc.GetElementsByTagName("title");
            XmlNodeList comDesc = xmlDoc.GetElementsByTagName("description");
            XmlNodeList comDate = xmlDoc.GetElementsByTagName("pubDate");
            XmlNodeList comAuthor = xmlDoc.GetElementsByTagName("creator");

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Title: " + comTitle[0].InnerText + "\n");
            sb.AppendLine("Desciption: " + comDesc[0].InnerText + "\n");
            sb.AppendLine("Date: " + comDate[0].InnerText + "\n");
            sb.AppendLine("Author: " + comAuthor[0].InnerText + "\n" + "---------------" + "\n");

            richComResults.Text = sb.ToString();
        }
    }

我的XML文件:

<channel>
<item>
      <title>Re: My view of Tesco</title>
      <description>
         <![CDATA[ Stay clear of the iii IPO when it comes onto the market.  3 quarters are multiples, triples, quadrupole, W-T-F.  It´s like ebay a lot bidding there, is fake too.Today´s thought of the day:  Odd is that Deloitte seems to have escaped headlines. Accusation it colluded with Standard Chartered on regulatory report cd be Enron moment  Remember those old accountants Deloitte, Mr. Hyman (RBS)? By Hardcore Uproar ]]>
      </description> 
      <pubDate>Tue, 07 Aug 2012 14:03:00 GMT</pubDate>
      <creator>Hardcore Uproar</creator>
</item>
<item>
      <title></title>
      <description>
         <![CDATA[ Raw material inflation;
         Rising (relative) 
         wealth outside of EU.
         Increased global demand for agri-commodities due to increasing population and relative wealth of Eastern countries.
         Decoupling of subsidy from agri-production = bad for supermarkets.
         Weather problems, diminished /(ing) resources and a general plateau reached in agriculture in terms of yield achievable = limited supply.
         Over supply of supermarkets/ retailers (too much choice= supply>demand)
         Diminished disposable income; 
         General recession.
         Poor pension performance.
         Over indebtidness in UK (further compounded by any increases in interest rates required to curb inflation).

         All this is bad news for supermarkets.. in my locality in a farily small town of 14,000 people we have a large ASDA, huge TESCO and M and S and numerous discounters.. they must be counting on all 14000 of those people visiting ALL of their local supermarkets at least 9 times a week IMHO!!
          By t8vet ]]>
       </description>
      <pubDate>Mon, 06 Aug 2012 18:47:00 GMT</pubDate>
      <creator>t8vet</creator>
</item>
</channel>

应用你的(代号)后我编辑的代码:

private void btnComSearch_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
            xmlDoc.Load("tsco.xml"); //* load the XML document from the specified file.

            XmlElement root = xmlDoc.DocumentElement;
            XmlNodeList nodes = root.SelectNodes("item"); // You can also use XPath here
            foreach (XmlNode node in nodes)
            {
                StringBuilder sb = new StringBuilder();
                foreach (XmlNode child in node.ChildNodes)
                    sb.AppendLine(string.Format("{0}:\t{1}", child.Name, child.FirstChild == null ? string.Empty : child.FirstChild.Value));
                richComResults.Text = sb.ToString();
            }
            Console.ReadLine();
        }

我完全迷失了。我尝试浏览网站,也有人提出类似的问题,但我真的不明白,我试过他们不适用于我的情况。我不确定我做错了什么。非常感谢你的帮助:)非常感谢你。

1 个答案:

答案 0 :(得分:1)

您正在观看xmlDoc,而您应该观看每个节点。试试这个:

        XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
        xmlDoc.Load("tsco.xml"); //* load the XML document from the specified file.

        richComResults.Text = string.Empty;

        XmlElement root = xmlDoc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("item"); // You can also use XPath here
        StringBuilder sb = new StringBuilder();

        foreach (XmlNode node in nodes)
        {                
            foreach (XmlNode child in node.ChildNodes)
                sb.AppendLine(string.Format("{0}:\t{1}", child.Name,  child.FirstChild == null ? string.Empty : child.FirstChild.Value));                
        }
        richComResults.Text = sb.ToString();