Timeout on Xml url download using XmlDocument Load, C

J

jeff3048

Hi,

I am writing a c# .net Visual Studio 2005 console application that retrieves
RSS feed xml from various web sources (urls). I have been told that an
exception thrown in a console application terminates its scheduled operation
so I have to be careful in my code to trap any errors. It is conceivable to
me that while downloading an xml document from a url that there may be
trouble at the other end causing an excessive delay in file download. I want
to be able to set a timeout on the file download so that my routine can skip
the current xml document if the timeout is exceeded and proceed to the next
url/xml. I am very new to xml stuff and don’t really know where to begin to
implement a timeout on the XmlDocument Load object. Can anyone provide some
guidance? My search on the web touched some reference to using "XmlResolver,
where you can set timeout on httpwebrequest" but no implementation. Any help
will be appreciated.

Thank you,
Jeff


My Pseudo code
int j = 0;
foreach (strFeedURL in my list of urls)
{
//Example: strFeedURL =
@"http://hosted.ap.org/lineups/POLITICSHEADS-rss_2.0.xml?SITE=PASTR&SECTION=HOME";

XmlDocument xDoc = new XmlDocument();
xDoc.Load(strFeedURL);

XmlPutFilePathName = c:\rssxml\rssfeed" + Convert.ToString(j) + ".xml";
xDoc.Save(XmlPutFilePathName);

j++;
}
 
N

Nicholas Paldino [.NET/C# MVP]

Jeff,

Well, first off, if an exception is left uncaught, then it will end up
tearing down the app domain, which in the case of a console or windows
program, will cause the program to stop.

As for specifying the timeout, the XmlResolver class isn't going to help
you. Rather, what you want to do is look at the HttpWebRequest and
HttpWebResponse classes. These classes will allow you to download your
content, as well as specify a timeout. Then, once you have that content,
feed it to the XmlDocument class, instead of having the XmlDocument class
load it for you.
 
M

Martin Honnen

jeff3048 said:
I am writing a c# .net Visual Studio 2005 console application that retrieves
RSS feed xml from various web sources (urls). I have been told that an
exception thrown in a console application terminates its scheduled operation
so I have to be careful in my code to trap any errors. It is conceivable to
me that while downloading an xml document from a url that there may be
trouble at the other end causing an excessive delay in file download. I want
to be able to set a timeout on the file download so that my routine can skip
the current xml document if the timeout is exceeded and proceed to the next
url/xml. I am very new to xml stuff and don’t really know where to begin to
implement a timeout on the XmlDocument Load object. Can anyone provide some
guidance? My search on the web touched some reference to using "XmlResolver,
where you can set timeout on httpwebrequest" but no implementation. Any help
will be appreciated.

You can set the Timeout property of an HttpWebRequest object, then call
GetResponse and access its response stream using GetResponseStream()
which you pass to the Load method of an XmlDocument instance.

Note also that XmlDocument Load/Save is not necessary if you simply want
to parse and save the XML you receive, you can instead use an XmlReader
and an XmlWriter where you simply use the WriteNode method of the
XmlWriter and pass in your XmlReader:
using (XmlReader reader =
XmlReader.Create(httpWebResponseInstance.GetResponseStream()))
{
using (XmlWriter writer = XmlWriter.Create(@"feed.xml"))
{
writer.WriteNode(reader, false);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top