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

  • Thread starter Patrick Olurotimi Ige
  • Start date
P

Patrick Olurotimi Ige

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.GetResponseStream());
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(myReader);
getXML = doc;




myResponse.Close();
myReader.Close();
}
 
C

Chris R. Timmons

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.GetResponseStream());
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");
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top