Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Services > There was an error generating the XML document. --> Object reference not set to an instance of an object

Reply
Thread Tools

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

 
 
fraser.elder@gmail.com
Guest
Posts: n/a
 
      12-01-2005
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.SoapDocumentMethodAt tribute("http://tempuri.org/GetFolder",
RequestNamespace="http://tempuri.org/",
ResponseNamespace="http://tempuri.org/",
Use=System.Web.Services.Description.SoapBindingUse .Literal,
ParameterStyle=System.Web.Services.Protocols.SoapP arameterStyle.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\\L ogFile.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

 
Reply With Quote
 
 
 
 
Peter Kelcey
Guest
Posts: n/a
 
      12-01-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
Error:Object reference not set to an instance of an object. Suresh Kojhani ASP .Net 1 07-29-2004 12:10 PM
Error !Object reference not set to an instance of an object. !!! Help Parthiv Joshi ASP .Net 2 07-02-2004 10:28 AM
Dropdown list + XML = Object reference not set to an instance of an object. darrel ASP .Net 0 04-27-2004 03:42 PM
HELP! Error Loading ASPX : Object Reference not set to an instance object Pedro Correia ASP .Net 0 07-25-2003 10:42 AM



Advertisments