?
Solved

looping a XmlTextWriter object

Posted on 2004-10-16
3
Medium Priority
?
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Assisted Solution

by:Jigit
Jigit earned 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

764 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