0

我正在做我的项目,我对 C# 完全陌生,我需要使用 C# 来完成我的工作。我真的希望你们能在下面的问题中帮助我。您的帮助将不胜感激!!谢谢。

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

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

说明:当它进入市场时,请远离 iii IPO。3个季度是倍数,三倍,四极,WTF。就像 ebay 那里有很多竞价一样,也是假的。

今天的想法:奇怪的是,德勤似乎逃脱了头条新闻。指控它与渣打勾结监管报告 cd 被安然时刻 还记得那些老会计师德勤、海曼先生(RBS)吗?通过 铁杆骚动

日期:格林威治标准时间 2012 年 8 月 7 日星期二 14:03:00

作者:铁杆骚动

我的代码如下:

        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>

应用您的(horgh)代码后我的编辑代码:

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();
        }

我完全迷路了。我尝试浏览网站,也有人问类似的问题,但我不太明白,我试过它们在我的情况下不起作用。我不确定我做错了什么。非常感谢您的帮助:) 非常感谢。

4

1 回答 1

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();            
于 2012-08-08T02:42:49.270 回答