Loading an XML document using WebMethod of Web Service

C

Csharp?

-- My code is listed as follows.

public class WebService1 : System.Web.Services.WebService
{
public WebService1()
[WebMethod(CacheDuration = 50,
Description="Load XML Document")]
public DataSet GetBigData()
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.Load(Server.MapPath("C:\WebServices\UseData\UseData1.xml"));
System.Xml.Xsl.XslTransform trans = new
System.Xml.Xsl.XslTransform();
trans.Load(Server.MapPath("MyStyle.xsl"));
Xml1.Document = doc;
Xml1.Transform = trans;
}
}

I got error messages of "Unrecognized escape
sequence C:\WebServices\WebService1.cs". Apparently, it did not like "\".

Can someone help me with this code and make it work?
 
N

Nicholas Paldino [.NET/C# MVP]

Well, there are two issues here.

The first is the string you used. The \ is an escape sequence
indicator, so your string should look like this:

C:\\WebServices\\UseData\\UseData1.xml

However, that still won't work really, as you are passing that to
MapPath. MapPath will return the physical file path on the server for a the
specified virtual path on the server. Passing it a file path on the server
won't do anything for you, just pass that directly.
 
C

Csharp?

Thank you, Nicholas.

Basically, I would like to load an xml file,usecase1.xml, located under the
directory of C:\WebServices\UseData\ through web service. How can I achieve
that?

Thank you for your help! :)

--
Csharp?


Nicholas Paldino said:
Well, there are two issues here.

The first is the string you used. The \ is an escape sequence
indicator, so your string should look like this:

C:\\WebServices\\UseData\\UseData1.xml

However, that still won't work really, as you are passing that to
MapPath. MapPath will return the physical file path on the server for a the
specified virtual path on the server. Passing it a file path on the server
won't do anything for you, just pass that directly.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Csharp? said:
-- My code is listed as follows.

public class WebService1 : System.Web.Services.WebService
{
public WebService1()
[WebMethod(CacheDuration = 50,
Description="Load XML Document")]
public DataSet GetBigData()
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.Load(Server.MapPath("C:\WebServices\UseData\UseData1.xml"));
System.Xml.Xsl.XslTransform trans = new
System.Xml.Xsl.XslTransform();
trans.Load(Server.MapPath("MyStyle.xsl"));
Xml1.Document = doc;
Xml1.Transform = trans;
}
}

I got error messages of "Unrecognized escape
sequence C:\WebServices\WebService1.cs". Apparently, it did not like "\".

Can someone help me with this code and make it work?
 
N

Nicholas Paldino [.NET/C# MVP]

Just use this string "C:\\WebServices\\UseData\\usecase1.xml" and do not
make the call to MapPath, just pass the string directly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Csharp? said:
Thank you, Nicholas.

Basically, I would like to load an xml file,usecase1.xml, located under
the
directory of C:\WebServices\UseData\ through web service. How can I
achieve
that?

Thank you for your help! :)

--
Csharp?


Nicholas Paldino said:
Well, there are two issues here.

The first is the string you used. The \ is an escape sequence
indicator, so your string should look like this:

C:\\WebServices\\UseData\\UseData1.xml

However, that still won't work really, as you are passing that to
MapPath. MapPath will return the physical file path on the server for a
the
specified virtual path on the server. Passing it a file path on the
server
won't do anything for you, just pass that directly.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Csharp? said:
-- My code is listed as follows.

public class WebService1 : System.Web.Services.WebService
{
public WebService1()
[WebMethod(CacheDuration = 50,
Description="Load XML Document")]
public DataSet GetBigData()
{
System.Xml.XmlDocument doc = new
System.Xml.XmlDocument();

doc.Load(Server.MapPath("C:\WebServices\UseData\UseData1.xml"));
System.Xml.Xsl.XslTransform trans = new
System.Xml.Xsl.XslTransform();
trans.Load(Server.MapPath("MyStyle.xsl"));
Xml1.Document = doc;
Xml1.Transform = trans;
}
}

I got error messages of "Unrecognized escape
sequence C:\WebServices\WebService1.cs". Apparently, it did not like
"\".

Can someone help me with this code and make it work?
 
P

Peter Bromberg [C# MVP]

Aside from the pathing issues that Nick described, your Webmethod is supposed
to return a DataSet but it appears that the farthest you have gotten is to
perform an XSL transform on a loaded XmlDocument. Not quite a DataSet yet...
-- Peter
Site: www.eggheadcafe.com
UnBlog: petesbloggerama.blogspot.com
Metafinder: www.blogmetafinder.com
 
C

Csharp?

Thank you, Peter.

Now I change my code as follows.


public class WebService1 : System.Web.Services.WebService
{
public WebService1()
[WebMethod(CacheDuration = 50,
Description="Load XML Document")]
public DataSet GetBigData()
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.Load("C:\\WebServices\\UseData\\UseData1.xml");

}
}

I got an error message of 'MyService.WebService1.GetBigData()': not all code
paths return a value C:\WebServices\WebService1.cs

Can you please help?

Thanks.

--
Csharp?


Peter Bromberg said:
Aside from the pathing issues that Nick described, your Webmethod is supposed
to return a DataSet but it appears that the farthest you have gotten is to
perform an XSL transform on a loaded XmlDocument. Not quite a DataSet yet...
-- Peter
Site: www.eggheadcafe.com
UnBlog: petesbloggerama.blogspot.com
Metafinder: www.blogmetafinder.com



Csharp? said:
-- My code is listed as follows.

public class WebService1 : System.Web.Services.WebService
{
public WebService1()
[WebMethod(CacheDuration = 50,
Description="Load XML Document")]
public DataSet GetBigData()
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.Load(Server.MapPath("C:\WebServices\UseData\UseData1.xml"));
System.Xml.Xsl.XslTransform trans = new
System.Xml.Xsl.XslTransform();
trans.Load(Server.MapPath("MyStyle.xsl"));
Xml1.Document = doc;
Xml1.Transform = trans;
}
}

I got error messages of "Unrecognized escape
sequence C:\WebServices\WebService1.cs". Apparently, it did not like "\".

Can someone help me with this code and make it work?
 
A

Arne Vajhøj

Csharp? said:
Now I change my code as follows.

public class WebService1 : System.Web.Services.WebService
{
public WebService1()
[WebMethod(CacheDuration = 50,
Description="Load XML Document")]
public DataSet GetBigData()
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.Load("C:\\WebServices\\UseData\\UseData1.xml");

}
}

I got an error message of 'MyService.WebService1.GetBigData()': not all code
paths return a value C:\WebServices\WebService1.cs

You do not return a DataSet which the method signature say you should.

But before you create a DataSet from the XML then consider this: why
convert to DataSet server side - why not just send the XML as a String
to the client and let the client load it into a DataSet ?

Arne
 

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

Top