Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 2nd, 2011, 08:51 AM
Authorized User
 
Join Date: Apr 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default not able to access xml file in webservice

i am using two files in my webservice
one is the service.cs (webservice code)
a class file(handset.cs)

the problem is when i call the webservice method :detect()

iam getting error as follows


System.UnauthorizedAccessException: Access to the path 'D:\HARSHIL\asp.net tutorials\webservice1' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext)
at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings)
at System.Xml.Linq.XDocument.Load(String uri, LoadOptions options)
at System.Xml.Linq.XDocument.Load(String uri)
at handset.getscreen(String mod) in d:\HARSHIL\asp.net tutorials\webservice1\App_Code\handset.cs:line 158
at handset.uadetect(String ua) in d:\HARSHIL\asp.net tutorials\webservice1\App_Code\handset.cs:line 59
at Service.detect(String ua) in d:\HARSHIL\asp.net tutorials\webservice1\App_Code\Service.cs:line 32







here's the code for my Service.cs file
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Data;
using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    handset hs = new handset();
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    
    [WebMethod]
    public string HelloWorld(string id) {
        return "Hello World"+id;
    }
    [WebMethod]
    public string detect(string ua)
    {
        DataSet ds= hs.uadetect(ua);
        string str = "Model: " + ds.Tables[0].Rows[0][0].ToString() + " Size: " + ds.Tables[0].Rows[0][0].ToString();
        return str;
    }


Here's my code for handset.cs class file that i am using
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for handset
/// </summary>
public class handset
{
    int n = -1;
    int end = -1;
    int sl = -1;
    int dot = -1;
    string str = "";
    
	public handset()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public string modelNo { set; get; }
    public string screen { set; get; }
    public string manu { set; get; }
    public string xmlfile { set; get; }
    public string scrfile { set; get; }

    public DataSet uadetect(string ua)
    {
        string man = getmanu(ua);
        DataSet ds = new DataSet();
        //
        string m = "";
        
        switch (man)
        {
            case "Nokia":
                m = nokiadet(ua);
                break;
            case "SonyEricsson":
                m = sonydet(ua);
                break;
            case "BlackBerry":
                m = bbdet(ua);
                break;
            case "LG":
                m = lgdet(ua);
                break;
        }

        string scr = getscreen(m);
        string xmlstr = "<model>" + m + "</model><ScreenSize>" + scr + "</ScreenSize>";
        using (StringReader sr = new StringReader(xmlstr))
        {
            ds.ReadXml(sr);
        }
        return ds;
        //
        return ds;
    }

    public string nokiadet(string ua)
    {
        string model = null;
        if (ua.IndexOf("NokiaC") >= 0 || ua.IndexOf("NokiaX") > 0)
        {
            n = ua.IndexOf("Nokia");
            sl = ua.IndexOf("/");
            str = ua.Substring(n, sl);
            dot = str.IndexOf(".");
            if (dot > 0)
                model = str.Substring(0, dot);
            else
                model = str;
            string str1 = "Nokia" + " " + model.Substring(5);
            model = str1;
        }
        else
        {
            n = ua.IndexOf("Nokia");
            sl = ua.IndexOf("/");

            str = ua.Substring(n, sl + 1);
            end = str.IndexOf("-");
            if (str.IndexOf("-") >= 0)
                model = str.Substring(0, end);
            else
                model = str;
            int i = model.IndexOf("kia");
            i = i + 4;
            int len = model.Length;

            string str1 = "Nokia" + " " + model.Substring(5);
            model = str1;
        }
        return model;
    }

    public string sonydet(string ua)
    {
        n = ua.IndexOf("SonyEricsson");
        string str1 = ua.Substring(n, 20);
        sl = str1.IndexOf("/");
        string str2 = str1.Substring(0, sl + 1);
        return str2;
    }

    public string lgdet(string ua)
    {
        n = ua.IndexOf("LG");
        string str1 = ua.Substring(n, 20);
        sl = str1.IndexOf("/");
        string str2 = str.Substring(0, sl + 1);
        return str2;
    }

    public string bbdet(string ua)
    {
        n = ua.IndexOf("BlackBerry");
        string str1 = ua.Substring(n, 20);
        sl = str1.IndexOf("/");
        string str2 = str1.Substring(0, sl);
        //str2 = "BlackBerry " + str2.Substring(10);
        return str2;
    }


    public Boolean avail(string mod)
    {
        XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(xmlfile));
        var q = from c in doc.Descendants("model")
                where c.Element("modelno").Value == mod
                select c.Element("screensize").Value;
        string m="";
        foreach (var x in q)
        {
            m = x;
        }
        if (m == null || m == "")
            return true;
        else
            return false;
    }




    public string getscreen(string mod)
    {
        XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(xmlfile));
        var q = from c in doc.Descendants("model")
                where c.Element("modelno").Value == mod
                select c.Element("screensize").Value;
        string scr = "";
        foreach (var x in q)
        {
            scr = x.ToString();
        }
        return scr;
    }
    public ArrayList getinfo(string mod)
    {
        ArrayList info = new ArrayList();
        XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(xmlfile));
        var q = from c in doc.Descendants("model")
                where c.Element("modelno").Value == mod
                select new { scr = c.Element("screensize").Value, man = c.Element("manu").Value };
        foreach (var x in q)
        {
            info.Add(x.scr);
            info.Add(x.man);
        }
        return info;
    }
    public string gethandsetlistbyscreen(string screen)
    {
        XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(xmlfile));
        var q = from c in doc.Descendants("model")
                where c.Element("screensize").Value == screen
                select new { model = c.Element("modelno").Value };
        string str = "";
        if (q != null)
        {
            foreach (var x in q)
            {
                str += x.model + ",";
            }
        }
        else
        {
            str = " ";
        }
        return str;
    }






    public ListItemCollection gethandsetlist(string man)
    {
        XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(xmlfile));
        var q=from c in doc.Descendants("model") 
                   where c.Element("manu").Value==man
                   select new {model=c.Element("modelno").Value};
        ListItemCollection ar = new ListItemCollection();
        foreach(var x in q)
        {
            ar.Add(new ListItem(x.model));
        }
        return ar;
    }

    public ListItemCollection getsizelist()
    {
        XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(scrfile));
        var q = from c in doc.Descendants("screen")
                select c.Value;
        ListItemCollection ar = new ListItemCollection();
        ar.Add(new ListItem("Select"));
        foreach (var x in q)
        {
            ar.Add(new ListItem(x.ToString()));
        }
        return ar;
    }


    public ListItemCollection getlist()
    {
        XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(xmlfile));
        var q = from c in doc.Descendants("model")
                select new { model = c.Element("modelno").Value };
        ListItemCollection ar = new ListItemCollection();
        ar.Add(new ListItem("Select"));
        foreach (var x in q)
        {
            ar.Add(new ListItem(x.model));
        }
        return ar;
    }



    public void save(string op,string mod,string sz)
    {
        XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(xmlfile));
        

        if (op == "update")
        {
            //update code
            XElement xe = doc.Descendants("model").Where(c => c.Element("modelno").Value == mod).FirstOrDefault();
            if (xe != null)
            {
                xe.SetElementValue("screensize", sz);
                doc.Save(HttpContext.Current.Server.MapPath(xmlfile));
            }
        }
        else if(op=="Insert")
        {
            //insert code
            XElement xe = new XElement("model", new XElement("modelno", modelNo), new XElement("screensize", screen), new XElement("manu", manu));
            doc.Element("Models").Add(xe);
            doc.Save(HttpContext.Current.Server.MapPath(xmlfile));

        }
    }
    public void delete(string mod)
    {
        XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(xmlfile));
        XElement xe = doc.Descendants("model").Where(c => c.Element("modelno").Value == mod).FirstOrDefault();
        xe.Remove();
        doc.Save(HttpContext.Current.Server.MapPath(xmlfile));
    }
    public string getmanu(string m)
    {
        int n = m.IndexOf("Nokia");
        int s = m.IndexOf("Sony");
        int l = m.IndexOf("LG");
        int mot = m.IndexOf("Motorola");
        int sa = m.IndexOf("Samsung");
        if (n >= 0)
            return "Nokia";
        else if (s >= 0)
            return "Sony";
        else if (l >= 0)
            return "LG";
        else if (mot >= 0)
            return "Motorola";
        else if (sa >= 0)
            return "Samsung";
        else
            return "Unknown";
    }
    public string modelrep(string modelist)
    {
        string str = modelist.Replace('.', ',');
        str = str.Replace(";", ",");
        str = str.Replace(" ,",",");
        str = str.Replace(", ", ",");
        return str;
    }
}

Last edited by harshil.sh; November 3rd, 2011 at 01:04 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Xml and Webservice ashokparchuri XML 4 December 8th, 2006 10:16 PM
ThePhile - Access Error to XML file cewekoolz BOOK: ASP.NET Website Programming Problem-Design-Solution 1 February 16th, 2005 02:16 AM
XML file access atingoldy Beginning VB 6 1 December 10th, 2003 09:18 AM
Send XML document to a webservice vinodveladanda XML 1 November 11th, 2003 11:46 AM
Simultaneous xml file access jim77002 XML 2 September 1st, 2003 03:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.