0

私は自分のプロジェクトに取り組んでいます。私はC#にまったく慣れていないので、C#を使用して作業を行う必要があります。皆さんが以下の号で私に手を貸してくれることを本当に願っています。よろしくお願いします!! ありがとう。

<item>...</item>親に2つあるXMLファイルがあります<channel>。私は自分のコードを実行しました、私が得るものは以下のとおりです-1つのデータが<item>現れました:

タイトル:Re:テスコに対する私の見解

説明:市場に出たときは、iiiIPOに近づかないでください。4分の3は、倍数、トリプル、四重極、WTFです。それはeBayがそこにたくさん入札しているようなもので、偽物でもあります。

今日の今日の考え:奇妙なことに、デロイトは見出しから逃れたようです。それが規制報告書でスタンダードチャータードと共謀した告発はエンロンの瞬間であるそれらの古い会計士デロイト、ハイマン氏(RBS)を覚えていますか?ハードコア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>

あなたの(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 に答える