Click here to Skip to main content
14,877,713 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends i have some problem in xml to json convert
please help me

i am using jquery to access the web method my script is as follows
JavaScript
$(document).ready(function () {
    $('#dlcustomers').change(function () {
        var obj = {};
        obj.cusID = $.trim($("[id*=dlcustomers]").val());
        alert(obj.cusID);
        $.ajax({
            type: "POST",
            url: "DataWebService.asmx/GetCustomersfn",
            data: JSON.stringify(obj),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                 // alert("sucesss");
                //alert(JSON.stringify(data));
                // alert(data.customers);
                $('#dataTable').html(JSON.stringify(data));
            },
            error: function (error) {
                alert("error");
            }
        });
    });
});


my web method is given below

C#
[WebMethod]
  // [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Xml)]
   public string GetCustomersfn(string cusID)
   {
       string query = "SELECT * FROM Customers WHERE CustomerID LIKE @CustomerID";
       string tablename = "Customers";
       SqlCommand cmd = new SqlCommand(query);
       cmd.Parameters.AddWithValue("CustomerID", "%" + cusID.Substring(0, 1) + "%");
       DataSet data = GetData(cmd, tablename);
      // return GetData(cmd, tablename).GetXml();
       System.IO.StringWriter writer = new System.IO.StringWriter();
       data.Tables[0].WriteXml(writer, XmlWriteMode.WriteSchema, false);
       return writer.ToString();

   }


i am getting output of the webservice in xml format as follows
<newdataset> <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="NewDataSet" msdata:isdataset="true" msdata:maindatatable="Customers" msdata:usecurrentlocale="true"> <xs:complextype> <xs:choice minoccurs="0" maxoccurs="unbounded"> <xs:element name="Customers"> <xs:complextype> <xs:sequence> <xs:element name="CustomerID" type="xs:string" minoccurs="0" /> <xs:element name="CompanyName" type="xs:string" minoccurs="0" /> <xs:element name="ContactName" type="xs:string" minoccurs="0" /> <xs:element name="ContactTitle" type="xs:string" minoccurs="0" /> <xs:element name="Address" type="xs:string" minoccurs="0" /> <xs:element name="City" type="xs:string" minoccurs="0" /> <xs:element name="Region" type="xs:string" minoccurs="0" /> <xs:element name="PostalCode" type="xs:string" minoccurs="0" /> <xs:element name="Country" type="xs:string" minoccurs="0" /> <xs:element name="Phone" type="xs:string" minoccurs="0" /> <xs:element name="Fax" type="xs:string" minoccurs="0" /> </xs:sequence> </xs:complextype> </xs:element> </xs:choice> </xs:complextype> </xs:element> </xs:schema> 
</newdataset>

<customers> <customerid>OLDWO</customerid> <companyname>Old World Delicatessen</companyname> <contactname>Rene Phillips</contactname> <contacttitle>Sales Representative</contacttitle> <address>2743 Bering St.</address> <city>Anchorage</city> <region>AK</region> <postalcode>99508</postalcode> <country>USA</country> <phone>(907) 555-7584</phone> <fax>(907) 555-2880</fax> </customers> <customers> <customerid>WANDK</customerid> <companyname>Die Wandernde Kuh</companyname> <contactname>Rita Müller</contactname> <contacttitle>Sales Representative</contacttitle> <address>Adenauerallee 900</address> <city>Stuttgart</city> <postalcode>70563</postalcode> <country>Germany</country> <phone>0711-020361</phone> <fax>0711-035428</fax> </customers> <customers> <customerid>WARTH</customerid> <companyname>Wartian Herkku</companyname> <contactname>Pirkko Koskitalo</contactname> <contacttitle>Accounting Manager</contacttitle> <address>Torikatu 38</address> <city>Oulu</city> <postalcode>90110</postalcode> <country>Finland</country> <phone>981-443655</phone> <fax>981-443655</fax> </customers> <customers> <customerid>WELLI</customerid> <companyname>Wellington Importadora</companyname> <contactname>Paula Parente</contactname> <contacttitle>Sales Manager</contacttitle> <address>Rua do Mercado, 12</address> <city>Resende</city> <region>SP</region> <postalcode>08737-363</postalcode> <country>Brazil</country> <phone>(14) 555-8122</phone> </customers> <customers> <customerid>WHITC</customerid> <companyname>White Clover Markets</companyname> <contactname>Karl Jablonski</contactname> <contacttitle>Owner</contacttitle> <address>305 - 14th Ave. S. Suite 3B</address> <city>Seattle</city> <region>WA</region> <postalcode>98128</postalcode> <country>USA</country> <phone>(206) 555-4112</phone> <fax>(206) 555-4115</fax> </customers> <customers> <customerid>WILMK</customerid> <companyname>Wilman Kala</companyname> <contactname>Matti Karttunen</contactname> <contacttitle>Owner/Marketing Assistant</contacttitle> <address>Keskuskatu 45</address> <city>Helsinki</city> <postalcode>21240</postalcode> <country>Finland</country> <phone>90-224 8858</phone> <fax>90-224 8858</fax> </customers> <customers> <customerid>WOLZA</customerid> <companyname>Wolski Zajazd</companyname> <contactname>Zbyszek Piestrzeniewicz</contactname> <contacttitle>Owner</contacttitle> <address>ul. Filtrowa 68</address> <city>Warszawa</city> <postalcode>01-012</postalcode> <country>Poland</country> <phone>(26) 642-7012</phone> <fax>(26) 642-7012</fax> </customers> 

but in the ajax call when i stringify the json object i am getting as follows
{"d":"\r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n OLDWO\r\n Old World Delicatessen\r\n Rene Phillips\r\n Sales Representative\r\n
2743 Bering St.
\r\n Anchorage\r\n AK\r\n 99508\r\n USA\r\n (907) 555-7584\r\n (907) 555-2880\r\n \r\n \r\n WANDK\r\n Die Wandernde Kuh\r\n Rita Müller\r\n Sales Representative\r\n
Adenauerallee 900
\r\n Stuttgart\r\n 70563\r\n Germany\r\n 0711-020361\r\n 0711-035428\r\n \r\n \r\n WARTH\r\n Wartian Herkku\r\n Pirkko Koskitalo\r\n Accounting Manager\r\n
Torikatu 38
\r\n Oulu\r\n 90110\r\n Finland\r\n 981-443655\r\n 981-443655\r\n \r\n \r\n WELLI\r\n Wellington Importadora\r\n Paula Parente\r\n Sales Manager\r\n
Rua do Mercado, 12
\r\n Resende\r\n SP\r\n 08737-363\r\n Brazil\r\n (14) 555-8122\r\n \r\n \r\n WHITC\r\n White Clover Markets\r\n Karl Jablonski\r\n Owner\r\n
305 - 14th Ave. S. Suite 3B
\r\n Seattle\r\n WA\r\n 98128\r\n USA\r\n (206) 555-4112\r\n (206) 555-4115\r\n \r\n \r\n WILMK\r\n Wilman Kala\r\n Matti Karttunen\r\n Owner/Marketing Assistant\r\n
Keskuskatu 45
\r\n Helsinki\r\n 21240\r\n Finland\r\n 90-224 8858\r\n 90-224 8858\r\n \r\n \r\n WOLZA\r\n Wolski Zajazd\r\n Zbyszek Piestrzeniewicz\r\n Owner\r\n
ul. Filtrowa 68
\r\n Warszawa\r\n 01-012\r\n Poland\r\n (26) 642-7012\r\n (26) 642-7012\r\n \r\n"}

how to properly access the xml data...help..
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900