C# 如何从api读取纯文本的响应体,并将其转换或序列化到.net core 2.0中的对象中

C# 如何从api读取纯文本的响应体,并将其转换或序列化到.net core 2.0中的对象中,c#,asp.net-core-mvc,C#,Asp.net Core Mvc,我试图从使用xpath构建的wsdl服务中读取返回的制表符明文。在.NETCore2.0中,如何将其反序列化为and对象或将其转换为对象。我使用.net核心内置wcf服务引用与wsdl服务对话 这是我在控制器级别的代码片段 [HttpPost] public async Task<IActionResult> Index(DateModelVm date) { try {

我试图从使用xpath构建的wsdl服务中读取返回的制表符明文。在.NETCore2.0中,如何将其反序列化为and对象或将其转换为对象。我使用.net核心内置wcf服务引用与wsdl服务对话

这是我在控制器级别的代码片段

[HttpPost]
        public async Task<IActionResult> Index(DateModelVm date)
        {
            try
            {

                if (ModelState.IsValid)
                {
                    if(date.PfaId == 21)
                    {
                        var depositVm = _DepositeModelPass.MapDepositeModelVmToModelVm(date, ICredentials._currentStanbicAccountNumber, ICredentials._currentStanbicCustomerId, ICredentials._currentzenithpasssowrd, ICredentials._currentzenithuserName);

                        var Mappedresult = _DepositeModelPass.MapDepositeModelVmToModel(depositVm);
            //fetch deposit from Servce
                        var ReturnedResult = await _depositService.getDepositsAsync(Mappedresult);



                        if (!string.IsNullOrEmpty(ReturnedResult.Body.result))
                        {

                            // create folder path
                            string webRootPath = _hostingEnvironment.WebRootPath;

                            // create directory
                            var directoryname = ProcessFile.CreateDirectory(webRootPath);



                            //create  write into text file
                            await ProcessFile.CreateAndWriteToTextFile(directoryname, ReturnedResult.Body.result);



                            List<DepositModel> processedDeposit = CsvMapper.DepositModelParser(directoryname);
}




使用csvhelper库从文本文件提取以转换为对象


 public static List<DepositModel> DepositModelParser(string directoryName)
        {
            string path = directoryName + "\\" + "xxxx.txt";
            List<DepositModel> depositModels = new List<DepositModel>();
            // read from file
            using (StreamReader str = System.IO.File.OpenText(path))
            using (CsvReader csvReader = new CsvReader(str))
            {
                IEnumerable<DepositModel> deposits = csvReader.GetRecords<DepositModel>();
                csvReader.Configuration.Delimiter = "\t";
                csvReader.Configuration.TrimOptions = TrimOptions.Trim;
                csvReader.Configuration.RegisterClassMap<DepositeModelMap>();
                csvReader.Configuration.MissingFieldFound = null;


                foreach (var item in deposits)
                {
                    var depo = new DepositModel()
                    {
                        AccountNumber = item.AccountNumber,
                        Amount = item.Amount,
                        Branch = item.Branch,
                        City = item.City,
                        Country = item.Country,
                        Currency = item.Currency,
                        DepositSlipNumber = item.DepositSlipNumber,
                        Description = item.Description,
                        FirstName = item.FirstName,
                        FundId = item.FundId,
                        LastName = item.LastName,
                        OtherName = item.OtherName,
                        PaymentMethod = item.PaymentMethod,
                        PFACode = item.PFACode,
                        PFAName = item.PFAName,
                        ReversalNumber = item.ReversalNumber,
                        RSAPin = item.RSAPin,
                        TransactionDate = item.TransactionDate,
                        TransactionNumber = item.TransactionNumber,
                        ValueDate = item.ValueDate

                    };
                    depositModels.Add(depo);

                }
            }
}



有没有一种方法可以直接读取此文件并转换为对象,而无需使用csv帮助程序库,也无需在从wsdl服务获取时写入文本文件…

Hi tmax!你能给我们看看你已经试过的吗?您能将纯文本转换成字符串变量吗?如果是,字符串是什么类型的内容?XML?JSON?还有什么吗?谢谢你在caseyCrookston的快速回复。我现在不在办公室,明天我将发送示例代码。但内容并不是真正的XML或由制表符分隔的纯文本内容。这就是服务返回的内容,这就是a试图转换为对象以便我可以分配给我的模型的内容。好的,那么你能将制表符分隔的文本转换为字符串变量吗?是的,在CaseyCrokston。我可以把它写进一个字符串变量,也可以把它写进一个文本文件。好的!所以,您真正的问题是:如何将制表符分隔的字符串反序列化为对象?:-)如果您可以向我们显示字符串,并向我们显示要从中创建的对象,这将非常有用。

 public static List<DepositModel> DepositModelParser(string directoryName)
        {
            string path = directoryName + "\\" + "xxxx.txt";
            List<DepositModel> depositModels = new List<DepositModel>();
            // read from file
            using (StreamReader str = System.IO.File.OpenText(path))
            using (CsvReader csvReader = new CsvReader(str))
            {
                IEnumerable<DepositModel> deposits = csvReader.GetRecords<DepositModel>();
                csvReader.Configuration.Delimiter = "\t";
                csvReader.Configuration.TrimOptions = TrimOptions.Trim;
                csvReader.Configuration.RegisterClassMap<DepositeModelMap>();
                csvReader.Configuration.MissingFieldFound = null;


                foreach (var item in deposits)
                {
                    var depo = new DepositModel()
                    {
                        AccountNumber = item.AccountNumber,
                        Amount = item.Amount,
                        Branch = item.Branch,
                        City = item.City,
                        Country = item.Country,
                        Currency = item.Currency,
                        DepositSlipNumber = item.DepositSlipNumber,
                        Description = item.Description,
                        FirstName = item.FirstName,
                        FundId = item.FundId,
                        LastName = item.LastName,
                        OtherName = item.OtherName,
                        PaymentMethod = item.PaymentMethod,
                        PFACode = item.PFACode,
                        PFAName = item.PFAName,
                        ReversalNumber = item.ReversalNumber,
                        RSAPin = item.RSAPin,
                        TransactionDate = item.TransactionDate,
                        TransactionNumber = item.TransactionNumber,
                        ValueDate = item.ValueDate

                    };
                    depositModels.Add(depo);

                }
            }
}


Transaction No. Transaction Date Value Date Description Amount Currency Branch City Country Reversal No. Payment Method Deposit Slip No RSA PIN First Name Last Name Other Name PFA Code PFA Name Fund ID Account No
68663XXX 2019-09-10 11:35:24 2019-09-10 XXXX110003XXXX/TEXXX/NYXXX/028/DS 76026 CD K INNOVATIVE 500.00 NGN Eagle Abuja NGR   0   76026 XXX110003XXXX TERKIMBI NYIEYEM   028 TRUSTXXX_XXXX 16 10163XXXX
d639164fe718df68c18aacbXXX​