read XML with more than one child node

Last post 07-06-2008 4:03 PM by newbie131. 3 replies.

Sort Posts:

  • read XML with more than one child node

    07-05-2008, 2:00 AM
    • Member
      2 point Member
    • newbie131
    • Member since 07-05-2008, 1:42 AM
    • Posts 5

    Hi,

    I'm new in using XML and was wondering how to read multiple items in an XML FIle using asp.net C#

    <newsletter>
    <items value="article">
      <item>
       <image>car.gif</image>
       <text>text goes here</text>
       <author>bdillan/author>
       <date>070408</date>
    </item>
      <item>
       <image>car2.gif</image>
       <text>text goes here</text>
       <author>bdillan/author>
       <date>070408</date>
    </item>
    </items>

    <items value="comics">
      <item>
       <image>comic.gif</image>
       <text>text goes here</text>
       <author>bdillan/author>
       <date>070408</date>
    </item>
      <item>
       <image>comic2.gif</image>
       <text>text goes here</text>
       <author>bdillan/author>
       <date>070408</date>
    </items>
    </newsletter>


    This is the code I have so far:

    public static void getNewsletter()
    {
    XMLDocument objGetNews = new XMLDocument();
    objGetNews.Load(HttpContext.Current.Server.MapPath("newsletter.xml"));

    if (null != objGetNews)
    {
         foreach(XmlNode getNews in objGetNews)
             {
                    //this is where I get stuck
             }
    }

     }

     Any help would be much appreciated

  • Re: read XML with more than one child node

    07-05-2008, 7:30 AM
    • Member
      355 point Member
    • anupalavila
    • Member since 01-03-2008, 11:25 AM
    • Kerala, India
    • Posts 317

    Hi

    this link may help you http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx 

    Regards
    Anu Palavila
  • Re: read XML with more than one child node

    07-05-2008, 8:29 PM
    Answer
    • Participant
      1,879 point Participant
    • FarhanK
    • Member since 01-14-2008, 5:21 PM
    • Posts 254

    HI,

    I am not sure what exaclty you want to read from your file but here is a generic funtion for you that will read you xml data. You can also visit http://www.developer.com/net/csharp/article.php/3489611 to learn on how to manipulate xml data.

         
        ReadNewsLetterXml(Server.MapPath("NewsLetter.xml"));

     

        public void ReadNewsLetterXml(string path)
        {
            System.Xml.XmlDocument Document = new System.Xml.XmlDocument();
            Document.Load(path);

            if (Document.HasChildNodes)
            {
                // This line below will enable you to read your full xml file
                ReadXmlNodes(Document.ChildNodes);
               
                // Uncomment the line below if you want to read article nodes
                // ReadXmlNodes(Document.SelectSingleNode("newsletter/items[@value='article']").ChildNodes);

                // Uncomment the line below if you want to read comic nodes
                // ReadXmlNodes(Document.SelectSingleNode("newsletter/items[@value='comics']").ChildNodes);

            }
        }


        /// <summary>
        /// This function will read the xmlnodelist selected
        /// </summary>
        /// <param name="nodeList"></param>
        public void ReadXmlNodes(System.Xml.XmlNodeList nodeList)
        {
            foreach (System.Xml.XmlNode n in nodeList)
            {
                if (n.ChildNodes.Count == 0)
                {
                    Response.Write(n.InnerText);
                    Response.Write("<br>");
                }
                else
                {
                    if (n.Attributes.Count > 0)
                    {
                        Response.Write("<b>" + n.Attributes[0].Value + "</b><br>");
                    }
                    ReadXmlNodes(n.ChildNodes);
                }
            }
        }

    [Please mark the post as answer that helps you.]

    Regards,
    Farhan Uddin Khan
    Enpointe Technologies
  • Re: read XML with more than one child node

    07-06-2008, 4:03 PM
    • Member
      2 point Member
    • newbie131
    • Member since 07-05-2008, 1:42 AM
    • Posts 5

    Thanks for the response Farhank.  This is what I've been trying to figure out and the link helped alot.

    Thanks!!!

Page 1 of 1 (4 items)