0

I want to check is object is empty or is NULL.

First I have a web method that take has input parameter XML document

[WebMethod(CacheDuration = 0, EnableSession=true, Description = "Učitaj dokument iz Aurore")]
public System.Xml.XmlDocument Load_DOK(System.Xml.XmlDocument XmlDoc)   //xml doc
{
}

In this method I have to check if XmlDoc is empty, if is throw error.

I wrote something like this:

try
{
    if( XmlDoc == null)
        errorMessage = "Input parameter is NULL!";
}
catch (Exception ex)
{
    WriteErrors.WriteToLogFile("WS.LOAD_DOK", ex.ToString());

    errorMessage = ex.Message;

    //Error exception
    soapEnvelop.LoadXml(@"<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><soap:Body><Response_status>1</Response_status><Description>" + ex.Message + "</Description></soap:Body></soap:Envelope>");
    return soapEnvelop;
}

I want to know is this the right way or is there a simpler way to do it?

4
  • 3
    if your try block has only the code you specified, then I don't think you will get any exception Commented Jul 25, 2012 at 10:07
  • What to you want the behaviour to be - that caller should get an exception? just the status? null? Commented Jul 25, 2012 at 10:08
  • So I have to write it differently? Commented Jul 25, 2012 at 10:09
  • Yes, If the caller doesn't fill the object he has to get an exception Commented Jul 25, 2012 at 10:09

1 Answer 1

4

Your try block should be something like

try 
{ 
    if( XmlDoc == null) 
    {
        throw new ArgumentNullException("XmlDoc");
    } 
    // carry on processing here.
}
Sign up to request clarification or add additional context in comments.

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.