convert xml to json format

Below codes response to xml format when jquery is called.
1. How can I code response to Json format?
2. More importantly, how to consume it?

#region GetNetPrice
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "ProQuoteService/GetNetPrice({CompanyID},{CustomerNo},{Model},{Qty},{OrderDate},{RepNo})")]
        String GetNetPrice(string CompanyID, string CustomerNo, string Model, string Qty, string OrderDate, string RepNo);
        #endregion

   public string GetNetPrice(string CompanyID, string CustomerNo, string Model, string Qty, string OrderDate, string RepNo)
        {
            Qty = Qty.Replace("^", ",");
            Model = Model.Replace("^", ",");

            OrderDate = "11/07/2014";
         
	    #region modelList
            if (Model.StartsWith("["))
            {
                Model = Model.Substring(1);
            }

            if (Model.EndsWith("]"))
            {
                Model = Model.Substring(0, Model.Length - 1);
            }
             
            #region qtyList
            if (Qty.StartsWith("["))
            {
                Qty = Qty.Substring(1);
            }

            if (Qty.EndsWith("]"))
            {
                Qty=Qty.Substring(0, Qty.Length - 1);
            }
            #endregion
            #region procedures
            try
            {
            string body = string.Format(@"<SOAP:Envelope><SOAP:Header></SOAP:Header>
            <SOAP:Body>
            <m:Retrieve>
            <itemid>
            <GenerateReportRequest><ReportType>CUST-NETPRICE</ReportType><ReportFormat>XML</ReportFormat>
            <ReportParams><CompanyNumber>{0}</CompanyNumber><CustomerNumber>{1}</CustomerNumber><Model>{2}</Model>
            <Quantity>{3}</Quantity><OrderDate>{4}</OrderDate><RepNumber>{5}</RepNumber><Delimiter>,</Delimiter>
            </ReportParams>
            </GenerateReportRequest>
            </itemid>
            </m:Retrieve>
            </SOAP:Body>
            </SOAP:Envelope>", CompanyID, CustomerNo, Model, Qty, OrderDate, RepNo);

                Usa.B2B.ERP.jBase.Helper.Retrieve d = new Usa.B2B.ERP.jBase.Helper.Retrieve(body);
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(d.GetData());                
                string node =  xml.SelectSingleNode("//RetrieveResult").InnerXml.Replace(@"\","");                                  
	        string status = xml.SelectSingleNode("//Status").InnerXml;
                string orderDate = xml.SelectSingleNode("//OrderDate").InnerXml;
                string customerNumber = xml.SelectSingleNode("//CustomerNumber").InnerXml;
                string model = xml.SelectSingleNode("//Model").InnerXml;
                string orderDetailList = xml.SelectSingleNode("//OrderDetail").InnerXml;
                return node;
            }
            catch (Exception ex)
            {
                //ILog log = LogManager.GetLogger("");
                //log.Error(m => m("Error:", ex.ToString()));
                throw ex;
            }
            #endregion
        }

Open in new window

LVL 1
ITsolutionWizardAsked:
Who is Participating?
 
Ronak PatelCommented:
Hi ITsolutionWizard,

You can use JsonConvert.SerializeXmlNode() method to convert xml string to Json string.
Please refer below code. I think it will solve your purpose.

XmlDocument doc = new XmlDocument();
doc.LoadXml(node);
string jsonText = JsonConvert.SerializeXmlNode(doc);

Open in new window


Reference Link: Converting between JSON and XML

You can get the required assemblies from here: Newtonsoft.Json


Hope this helps.

Regards,
Ronak
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.