By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,920 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,920 IT Pros & Developers. It's quick & easy.

Cannot implicitly convert type 'object' to 'System.Xml.XmlDocument' Error

P: n/a
compiling the code below i get the error:-

Cannot implicitly convert type 'object'to 'System.Xml.XmlDocument'

I'm getting the error on this line:-

myXml.Document = getXML("http://www.crn.com.au/rss.aspx?SCID=9");

Any ideas?

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

myXml.Document = getXML("http://www.crn.com.au/rss.aspx?SCID=9");
}

object getXML(string sourceFile)
{
System.Net.WebRequest myRequest =
System.Net.WebRequest.Create(sourceFile);
System.Net.WebResponse myResponse = myRequest.GetResponse();
System.Xml.XmlTextReader myReader = new
System.Xml.XmlTextReader(myResponse.GetResponseStr eam());
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(myReader);
getXML = doc;


myResponse.Close();
myReader.Close();
}
*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Patrick Olurotimi Ige <na********@hotmail.com> wrote in
news:ug**************@TK2MSFTNGP14.phx.gbl:
compiling the code below i get the error:-

Cannot implicitly convert type 'object'to
'System.Xml.XmlDocument'

I'm getting the error on this line:-

myXml.Document =
getXML("http://www.crn.com.au/rss.aspx?SCID=9");

Any ideas?

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

myXml.Document =
getXML("http://www.crn.com.au/rss.aspx?SCID=9");
}

object getXML(string sourceFile)
{
System.Net.WebRequest myRequest =
System.Net.WebRequest.Create(sourceFile);
System.Net.WebResponse myResponse =
myRequest.GetResponse(); System.Xml.XmlTextReader
myReader = new
System.Xml.XmlTextReader(myResponse.GetResponseStr eam());
System.Xml.XmlDocument doc = new
System.Xml.XmlDocument(); doc.Load(myReader);
getXML = doc;
myResponse.Close();
myReader.Close();
}


Patrick,

There are two modifications that need to be made.

1. In the getXML method, the return value is never set. The line of
code "getXML = doc;" will not compile. To return the XmlDocument,
change that line of code to "return doc;" and move it to the end of
the method.

2. The error message you are receiving is correct. You must perform
an explicit cast:

myXml.Document =
(XmlDocument) getXML("http://www.crn.com.au/rss.aspx?SCID=9");

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 19 '05 #2

P: n/a
It actually looks like you have a couple of problems:

1.) replace object getXML(string sourceFile) with
XmlDocument getXML(string sourceFile)
OR
repace myXml.Document = getXML("http://www.crn.com.au/rss.aspx?SCID=9");
with
myXml.Document =
(XmlDocument)getXML("http://www.crn.com.au/rss.aspx?SCID=9");

2.) replace getXML = doc; with
return doc; [and move it to the end of the method after you close
the Response and Reader objects]
HTH
----------------
Dave Fancher
http://davefancher.blogspot.com
Nov 19 '05 #3

P: n/a
Thx Dave and Chris for the quick response!
Need some C# brush up

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.