简体   繁体   中英

Convert XML <!CDATA to DataTable C#

I have Webservice SOAP method which takes Json as input argument.

//For sample
string jsonString = "[{"Year":"2020","UserId":"1","Comp":"20","DeptId":"32","CategoryId":"53","ItemId":"0"}]";

string[] processString = budget.BudgetTagNet(jsonString);

Now "budget.BudgetTagNet(jsonString)" returns Array of string as mentioned and the SOAP Response shows following value: SOAP RESPONSE RESULT

Now I want to convert the returned budget array to DataTable for further utilization for my project but I am unable to do. What could be the best way to achieve that. What I have tried so is mentioned below.

 string jsonString = CommonEnum.DataTableToJSON(Table);
 err.Message = "Json String is : " + jsonString;
 err.Insert();

 string[] processString = budget.BudgetTagNet(jsonString);
                    err.Message = "Budget service data is : " + processString[1];
                    err.Insert();

                    var XMLAsString = CommonEnum.SerializeXML(processString[1]);
                    err.Message = "Converted XML To String data is : " + 
   JsonConvert.SerializeObject(XMLAsString);
                    err.Insert();

                    dt = (DataTable) JsonConvert.DeserializeObject(XMLAsString, (typeof(DataTable)));
                    err.Message = "DataTable data : " + JsonConvert.SerializeObject(dt);
                    err.Insert();

And finally Data inserted in database as for the above method is: Database values inserted

You can use Regex to parse data:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =  "{\"Budkey\":2,\"BudTitle\":\"Budget Heads\", \"Budget_Amount\":0.0,\"Engaged\":0.0,\"Balance\":0.0";
            string pattern = @"""(?'key'[^""]+)"":(?'value'\w+)";
            MatchCollection matches = Regex.Matches(input, pattern);

            foreach (Match match in matches.Cast<Match>().Select(x => x))
            {
                Console.WriteLine("Key = '{0}', Value = '{1}'", match.Groups["key"].Value, match.Groups["value"].Value);
            }
            Console.ReadLine();
 

        }
    }
 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM