There was an error generating the XML document. --> Object reference not set to an instance of an ob

Discussion in 'ASP .Net Web Services' started by fraser.elder@gmail.com, Dec 1, 2005.

  1. Guest

    I've written a web service that returns an xml document with some
    information about documents in a Sharepoint Server, however when I try
    to consume the web service I get this error:

    Server was unable to process request. --> There was an error generating
    the XML document. --> Object reference not set to an instance of an
    object.
    Description: An unhandled exception occurred during the execution of
    the current web request. Please review the stack trace for more
    information about the error and where it originated in the code.

    Exception Details: System.Web.Services.Protocols.SoapException: Server
    was unable to process request. --> There was an error generating the
    XML document. --> Object reference not set to an instance of an object.

    Source Error:

    Line 35:
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetFolder",
    RequestNamespace="http://tempuri.org/",
    ResponseNamespace="http://tempuri.org/",
    Use=System.Web.Services.Description.SoapBindingUse.Literal,
    ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    Line 36: public System.Xml.XmlNode GetFolder(string strSite,
    string guidWebID, string strURL) {
    Line 37: object[] results = this.Invoke("GetFolder", new
    object[] {
    Line 38: strSite,
    Line 39: guidWebID,


    Source File: c:\inetpub\wwwroot\SPInterface\Web
    References\SPFileInfo\Reference.cs Line: 37




    My webmethod looks like this:

    [WebMethod]

    public XmlDocument GetFolder(string strSite, string guidWebID, string
    strURL)

    {

    StreamWriter sw = new
    StreamWriter("C:\\inetpub\\wwwroot\\SPInterface\\LogFile.txt",true);


    Guid guidWebID2 = new Guid(guidWebID);

    XmlDocument xml = new XmlDocument();


    //Find the Sharepoint site

    SPSite site = new SPSite(strSite);

    //Find the WebID on the Sharepoint site

    SPWeb web = site.OpenWeb(guidWebID2);

    //Find the folder we need

    SPFolder folder = web.GetFolder(strURL);


    if (folder.Exists)

    {

    //Get all the files in the folder

    SPFileCollection fileCollection = folder.Files;

    foreach (SPFile file in fileCollection)

    {

    sw.WriteLine("Adding details to array!");

    sw.WriteLine("Time : " + DateTime.Now.ToLongTimeString());

    sw.WriteLine("Created new document!");

    sw.WriteLine("Time : " + DateTime.Now.ToLongTimeString());

    XmlElement fileInfo = xml.CreateElement("FileInfo");

    sw.WriteLine("Created new element!");

    sw.WriteLine("Time : " + DateTime.Now.ToLongTimeString());




    fileInfo.SetAttribute("Name", file.Name.ToString());

    fileInfo.SetAttribute("FileURL", file.Url.ToString());

    fileInfo.SetAttribute("Site", strSite.ToString());

    fileInfo.SetAttribute("LastMod", file.TimeLastModified.ToString());

    fileInfo.SetAttribute("URL", strURL.ToString());



    sw.WriteLine("Adding attributes to element!");

    sw.WriteLine("Time : " + DateTime.Now.ToLongTimeString());


    }


    sw.Flush();

    sw.Close();


    return xml;

    }

    After I've added the attributes to the element do I need to do anything
    else to the XML document?

    My web service runs sucessfully (I know because it writes to the
    logfile at each step above).

    I'm consuming the web service by doing this:

    SPFileInfo.SPFileInfo fileInfo = new SPFileInfo.SPFileInfo();

    fileInfo.Credentials = System.Net.CredentialCache.DefaultCredentials;

    fileInfo.Url = strWSURL;


    XmlNode node = fileInfo.GetFolder(strSite, guidWebID, strURL);

    Am I missing anything obvious here? (This is my first go at a
    sharepoint interface and web services for that matter!)

    Any help is greatly appreciated!

    Cheers,

    Fraser
    , Dec 1, 2005
    #1
    1. Advertising

  2. Peter Kelcey Guest

    Fraser

    Your problem is caused by the way you are building your XML.
    You have to insert your fileInfo element into your xmldocument. The
    CreateElement doesn't automatically do that for you.
    When you are trying to return your XML object at the end, it is empty.

    Sample:

    XmlDocument xml = new XmlDocument();

    //I create a parent node first and put it as the first node, I do this
    //because the documentelement won't exist without it.
    xml.InsertAfter(xml.CreateElement("FileInfoParent"),null);

    //Now I add in the elements I want
    XmlElement fileInfo = xml.CreateElement("FileInfo");
    fileInfo.SetAttribute("Name", "asdf");
    fileInfo.SetAttribute("FileURL", "asdf");
    fileInfo.SetAttribute("Site", "asdf");
    fileInfo.SetAttribute("LastMod", "asdf");
    fileInfo.SetAttribute("URL", "asdf");
    //I can reference the documentelement here because of the first node I
    added
    xml.DocumentElement.AppendChild(fileInfo);

    fileInfo = xml.CreateElement("FileInfo");
    fileInfo.SetAttribute("Name", "asdf2");
    fileInfo.SetAttribute("FileURL", "asdf2");
    fileInfo.SetAttribute("Site", "asdf2");
    fileInfo.SetAttribute("LastMod", "asdf2");
    fileInfo.SetAttribute("URL", "asdf2");
    xml.DocumentElement.AppendChild(fileInfo);

    return xml;


    This code will produce the following XML

    <FileInfoParent>
    <FileInfo Name=\"asdf\" FileURL=\"asdf\" Site=\"asdf\"
    LastMod=\"asdf\" URL=\"asdf\" />
    <FileInfo Name=\"asdf2\" FileURL=\"asdf2\" Site=\"asdf2\"
    LastMod=\"asdf2\" URL=\"asdf2\" />
    </FileInfoParent>

    Hope that helps

    Peter Kelcey
    Peter Kelcey, Dec 1, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parthiv Joshi
    Replies:
    2
    Views:
    671
    Kalyan
    Jul 2, 2004
  2. Suresh Kojhani
    Replies:
    1
    Views:
    2,363
    Anushi
    Jul 29, 2004
  3. Pedro Correia
    Replies:
    0
    Views:
    613
    Pedro Correia
    Jul 25, 2003
  4. darrel
    Replies:
    0
    Views:
    420
    darrel
    Apr 27, 2004
  5. Replies:
    1
    Views:
    468
    Mark Rae [MVP]
    Sep 20, 2007
Loading...

Share This Page