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

F

fraser.elder

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
 
P

Peter Kelcey

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top