looping a XmlTextWriter object

Hello. This code does 2 things: parses RSS data into an html file and an xml file. The problem is in the xml document generation. It is only printing the first element. I'm pretty sure its an issue with the looping mechnism..but the html file generates perfectly, here's the xml generating portion of the code:

                      for (int i=0;i < maxItems;i++)
                  {
                        // initialize a XmlTextWriter object
                        XmlTextWriter objXmlWriter = null;

                        // path to the XML file
                        String strXmlFile = "C:/rss1/RssReaderApp/example.xml";

                        // start the "try" block
                        try
                        {

                              // create an instance of the XmlTextWriter object
                              objXmlWriter = new XmlTextWriter (strXmlFile , null);

                              // indent the output in the XML file
                              objXmlWriter.Formatting = Formatting.Indented;

                              // set the number of space to indent
                              objXmlWriter.Indentation =  5;

                              // start writing the XML document
                              objXmlWriter.WriteStartDocument(false);

                              // write a comment
                  

                              // start writing the elements
                              string title = Feed.Items[i].Title;
                              string description = Feed.Items[i].Description;
                              string link = Feed.Items[i].Link;
                              string author = Feed.Items[i].Author;
                              string comments = Feed.Items[i].Comments;
                              string pubdate = Feed.Items[i].Pubdate;
                              string guid = Feed.Items[i].Guid;


                              objXmlWriter.WriteStartElement("title", title);
                              objXmlWriter.WriteElementString("description", description);
                              objXmlWriter.WriteElementString("link", link);
                              objXmlWriter.WriteElementString("author", author);
                              objXmlWriter.WriteElementString("comments", comments);
                              objXmlWriter.WriteElementString("pubdate", pubdate);
                              objXmlWriter.WriteElementString("guid", guid);
                              objXmlWriter.WriteEndElement();
                              
                              // flush and write XML data to the file
                              objXmlWriter.Flush();

                        }
                        catch (XmlException e)
                        {

                              //output.Text = "An XML Exception occurred: " + e.Message;

                        }
                        catch (Exception e)
                        {

                              //output.Text = "A General Exception occurred: " + e.Message;

                        }
                        finally
                        {

                              // close the XMLWriter object
                              if(objXmlWriter != null)
                              {
                                    objXmlWriter.Close();
                              }  
                        }        
                  
Here's the complete method, I put the above portion seprate because I pasted the xmltextwriter code, within the logic of the rss html generator code, from another source..sorry if I'm confusing anyone!


public string GetHtmlContents(RssFeed Feed,string Template,string ItemPrefix,string ItemSuffix)
            {
                  string result = Template;



                  // Replace all template tokens
                  result = result.Replace("%Title%",Feed.Title);
                  result = result.Replace("%Description%",Feed.Description);
                  result = result.Replace("%Link%",Feed.Link);
                  result = result.Replace("%Language%",Feed.Language);
                  result = result.Replace("%Copyright%",Feed.Copyright);
                  result = result.Replace("%Webmaster%",Feed.Webmaster);
                  result = result.Replace("%PubDate%",Feed.PubDate);
                  result = result.Replace("%LastBuildDate%",Feed.LastBuildDate);
                  result = result.Replace("%Category%",Feed.Category);
                  result = result.Replace("%Generator%",Feed.Generator);
                  result = result.Replace("%Ttl%",Feed.Ttl);
                  result = result.Replace("%Rating%",Feed.Rating);
                  result = result.Replace("%Skiphours%",Feed.Skiphours);
                  result = result.Replace("%Skipdays%",Feed.Skipdays);
                  result = result.Replace("%Skipdays%",Feed.ManagingEditor);

                  // Parse item template
                  string itemsContent = "";
                  string tempContent = "";

                  if ( maxItems == 0 || maxItems > Feed.Items.Count )
                  {
                        maxItems = Feed.Items.Count;
                  }

                  for (int i=0;i < maxItems;i++)
                  {
                        // initialize a XmlTextWriter object
                        XmlTextWriter objXmlWriter = null;

                        // path to the XML file
                        String strXmlFile = "C:/rss1/RssReaderApp/example.xml";

                        // start the "try" block
                        try
                        {

                              // create an instance of the XmlTextWriter object
                              objXmlWriter = new XmlTextWriter (strXmlFile , null);

                              // indent the output in the XML file
                              objXmlWriter.Formatting = Formatting.Indented;

                              // set the number of space to indent
                              objXmlWriter.Indentation =  5;

                              // start writing the XML document
                              objXmlWriter.WriteStartDocument(false);

                              // write a comment
                  

                              // start writing the elements
                              string title = Feed.Items[i].Title;
                              string description = Feed.Items[i].Description;
                              string link = Feed.Items[i].Link;
                              string author = Feed.Items[i].Author;
                              string comments = Feed.Items[i].Comments;
                              string pubdate = Feed.Items[i].Pubdate;
                              string guid = Feed.Items[i].Guid;


                              objXmlWriter.WriteStartElement("title", title);
                              objXmlWriter.WriteElementString("description", description);
                              objXmlWriter.WriteElementString("link", link);
                              objXmlWriter.WriteElementString("author", author);
                              objXmlWriter.WriteElementString("comments", comments);
                              objXmlWriter.WriteElementString("pubdate", pubdate);
                              objXmlWriter.WriteElementString("guid", guid);
                              objXmlWriter.WriteEndElement();
                              
                              // flush and write XML data to the file
                              objXmlWriter.Flush();

                        }
                        catch (XmlException e)
                        {

                              //output.Text = "An XML Exception occurred: " + e.Message;

                        }
                        catch (Exception e)
                        {

                              //output.Text = "A General Exception occurred: " + e.Message;

                        }
                        finally
                        {

                              // close the XMLWriter object
                              if(objXmlWriter != null)
                              {
                                    objXmlWriter.Close();
                              }  
                        }        
                  
                  
                        // Parse prefix template
                        tempContent  = ItemPrefix;
                        tempContent  = tempContent.Replace("%Title%",Feed.Items[i].Title);
                        tempContent  = tempContent.Replace("%Description%",Feed.Items[i].Description);
                        tempContent  = tempContent.Replace("%Link%",Feed.Items[i].Link);
                        tempContent  = tempContent.Replace("%Author%",Feed.Items[i].Author);
                        tempContent  = tempContent.Replace("%Comments%",Feed.Items[i].Comments);
                        tempContent  = tempContent.Replace("%Pubdate%",Feed.Items[i].Pubdate);
                        tempContent  = tempContent.Replace("%Guid%",Feed.Items[i].Guid);

                        itemsContent += tempContent;

                        // Parse suffix template
                        tempContent  = ItemSuffix;
                        tempContent  = tempContent.Replace("%Title%",Feed.Items[i].Title);
                        tempContent  = tempContent.Replace("%Description%",Feed.Items[i].Description);
                        tempContent  = tempContent.Replace("%Link%",Feed.Items[i].Link);
                        tempContent  = tempContent.Replace("%Author%",Feed.Items[i].Author);
                        tempContent  = tempContent.Replace("%Comments%",Feed.Items[i].Comments);
                        tempContent  = tempContent.Replace("%Pubdate%",Feed.Items[i].Pubdate);
                        tempContent  = tempContent.Replace("%Guid%",Feed.Items[i].Guid);

                        itemsContent += tempContent;
                  }

                  // Replace %items% with items
                  result = result.Replace("%Items%",itemsContent);

                  return result;
            }
            
      }


dprasadAsked:
Who is Participating?
 
der_jthConnect With a Mentor Commented:
You shouldn't initialize the XmlTextWriter inside the loop. Doing it causes the file to be wiped at the beginning of every element. The correct structure for your code should be something like this:

--
// initialize a XmlTextWriter object
XmlTextWriter objXmlWriter = null;

// path to the XML file
String strXmlFile = "C:/rss1/RssReaderApp/example.xml";

// create an instance of the XmlTextWriter object
objXmlWriter = new XmlTextWriter (strXmlFile , null);

// indent the output in the XML file
objXmlWriter.Formatting = Formatting.Indented;

// set the number of space to indent
objXmlWriter.Indentation =  5;

// start writing the XML document
objXmlWriter.WriteStartDocument(false);


for (int i=0;i < maxItems;i++) {

  // start writing the elements
  string title = Feed.Items[i].Title;
  string description = Feed.Items[i].Description;
  string link = Feed.Items[i].Link;
  string author = Feed.Items[i].Author;
  string comments = Feed.Items[i].Comments;
  string pubdate = Feed.Items[i].Pubdate;
  string guid = Feed.Items[i].Guid;

  objXmlWriter.WriteStartElement("title", title);
  objXmlWriter.WriteElementString("description", description);
  objXmlWriter.WriteElementString("link", link);
  objXmlWriter.WriteElementString("author", author);
  objXmlWriter.WriteElementString("comments", comments);
  objXmlWriter.WriteElementString("pubdate", pubdate);
  objXmlWriter.WriteElementString("guid", guid);
  objXmlWriter.WriteEndElement();  
}

objXmlWriter.Flush();
--

(try blocks and some other parts cut out for brevity; the key issue here is having the correct stuff in the loop)
0
 
JigitConnect With a Mentor Commented:
Try to change the last line of try block to the following:
objXmlWriter.Flush();
objXmlWriter.Close();
objXmlWriter = null;

And change the finnally block to the following:

finally
{
  if(objXmlWriter != null)
  {
    objXmlWriter.Flush();
    objXmlWriter.Close();
    objXmlWriter = null;
  }  
}        
               

0
 
dprasadAuthor Commented:
Thanks a lot guys, I found a more complete solution for what I need here for anyone interested

XML to SQL: Using SQLXML Bulkload in the .NET Framework
http://msdn.microsoft.com/sql/default.aspx?pull=/library/en-us/dnsql90/html/exchsqlxml.asp#exchsq_topic2
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.