Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit conversion from void to XmlDocument

Probably a stupid question, but I'm quite new to the whole "get-and-set-property"-kind of programming;

I keep getting a compiling-error on this part of my code;

private string _File = "Session.xml";

private XmlDocument XmlDoc
{
    get
    {
        XmlDocument _Doc = new XmlDocument();
        return _Doc.LoadXml(_File);
    }
}

private XmlElement XmlRoot
{
    get
    {
        return XmlDoc.DocumentElement;
    }
}

How come? I can't explain that to myself as I don't even see any implicit conversions...

like image 204
F.P Avatar asked May 09 '26 17:05

F.P


2 Answers

The problem is this line:

return _Doc.LoadXml(_File);

You're trying to return a value from a method that has a return type of void.

Try this instead:

private XmlDocument XmlDoc
{
    get
    {
        XmlDocument _Doc = new XmlDocument();
        _Doc.LoadXml(_File);
        return _Doc;
    }
}
like image 190
Kev Avatar answered May 12 '26 06:05

Kev


This code is your problem:

return _Doc.LoadXml(_File);

The LoadXml method has a return type of void, as the method does not return any value, instead populating the XmlDocument instance from the file path specified.

To fix your problem, simply change your property to this:

private XmlDocument XmlDoc
{
    get
    {
        XmlDocument _Doc = new XmlDocument();
        _Doc.LoadXml(_File);
        return _Doc;
    }
}
like image 25
Paul Turner Avatar answered May 12 '26 05:05

Paul Turner