Solved

looping a XmlTextWriter object

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

17 Experts available now in Live!

Get 1:1 Help Now