Solved

looping a XmlTextWriter object

Posted on 2004-10-16
3
492 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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