Solved

looping a XmlTextWriter object

Posted on 2004-10-16
3
490 Views
Last Modified: 2008-02-01
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;
            }
            
      }


0
Comment
Question by:dprasad
3 Comments
 
LVL 4

Assisted Solution

by:Jigit
Jigit earned 250 total points
ID: 12331729
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
 
LVL 6

Accepted Solution

by:
der_jth earned 250 total points
ID: 12334039
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
 

Author Comment

by:dprasad
ID: 12340597
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now