Answered Looping XML nodes Issue - C#

  • Tuesday, August 07, 2012 11:12 PM
     
      Has Code

    Hi,

    I am working on my project, I am totally new to C# and I need to use C# to do my work. I really hope you guys can lend me a hand in the below issue. Your help will be very much appreciated!! Thanks.

    I have an XML file as below:

    <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>

    I am trying to parse it. But I don't know how to loop through the <item></item>. It only shows one of the "item" like below, and it doesn't loop the second <item>.

    >>>>>>>>>>>

    Title: Re: My view of Tesco

    Desciption:  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 

    Date: Tue, 07 Aug 2012 14:03:00 GMT

    Author: Hardcore Uproar
    ---------------

    >>>>>>>>>>>>

    How can I get something like below for the two <item>?

    >>>>>>>>>>>>

    Title: Re: My view of Tesco

    Desciption:  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 

    Date: Tue, 07 Aug 2012 14:03:00 GMT

    Author: Hardcore Uproar
    ---------------

    Title: Re: My view of Tesco

    Desciption:  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 

    Date: Tue, 07 Aug 2012 14:03:00 GMT

    Author: Hardcore Uproar
    ---------------

    >>>>>>>>>>>>

    My Codes:

            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)
                {
                    //* Get elements.
                    XmlNodeList comTitle = xmlDoc.GetElementsByTagName("title");
                    XmlNodeList comDesc = xmlDoc.GetElementsByTagName("description");
                    XmlNodeList comDate = xmlDoc.GetElementsByTagName("pubDate");
                    XmlNodeList comAuthor = xmlDoc.GetElementsByTagName("creator");
    
                    //* Display the results into the rich textbox.
                    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();
                }
            }


    I appreciate all your helps and kind hearts! Regards, Shyuan


    • Edited by shyuanshyuan Wednesday, August 08, 2012 2:08 AM Forgot to include my codes
    •  

All Replies

  • Wednesday, August 08, 2012 8:26 AM
     
     Answered Has Code

    Hi,

    In fact you can use the Xpath(something like //item to loop each of the "item" and then use its ChildNodes:

    public class MainTest
        {
            static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("XMLFile1.xml");
                XmlNodeList  nodes = doc.SelectNodes("//item");
                foreach (XmlNode item in nodes)
                {
                    Console.WriteLine(item.ChildNodes[0].InnerText);
                    Console.WriteLine(item.ChildNodes[1].InnerText);
                    Console.WriteLine(item.ChildNodes[2].InnerText);
                    Console.WriteLine(item.ChildNodes[3].InnerText);
                    Console.WriteLine("=======");
                }
            }      
        }

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    • Marked As Answer by shyuanshyuan Wednesday, August 08, 2012 11:18 PM
    •  
  • Wednesday, August 08, 2012 11:19 PM
     
     
    Thank you 编程志愿者 :))

    I appreciate all your helps and kind hearts! Regards, Shyuan

  • Thursday, August 09, 2012 2:18 AM
     
     
    Thank you 编程志愿者 :))

    I appreciate all your helps and kind hearts! Regards, Shyuan


    Not at all!But welcome here to chat with us or u can help more……xD

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处