0

I have a class, in which I want to create an instance of an XDocument. In the constructor I need to call the "Load" method, but for some reason it is not available to call.

For example:

class MyClass
{
    private XDocument xmlResponse;

    public MyClass(string url)
    {
        xmlResponse.Load(url);
    }
}

I get the error "cannot be accessed with an instance reference; qualify it with a type name instead"

So I tried "MyClass.xmlResponse.Load(url)" but I'm getting the same error.

What is the proper way of calling the method?

3 Answers 3

6

The XDocument.Load method is static, so you have to call it statically. Try this:

public MyClass(string url)
{
    xmlResponse = XDocument.Load(url);
}

Further Reading

Sign up to request clarification or add additional context in comments.

Comments

3

You want:

public MyClass(string url)
{
    xmlResponse = XDocument.Load(url);
}

Load method is a static method on XDocument class, so you can't call it via an instance of XDocument.

Comments

-1
private XDocument xmlResponse;
xmlResponse = new XDocument(); 

try this. object has to be created before accessing xmlResponse

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.