Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I getting data in my application through RFC. RFC returns data in several parameters/variables. One of the parameter return below string.
STRUCTURE ERRMSG{FIELD TYPE=E 
		FIELD ID = 
		FIELD NUMBER=000 
		FIELD MESSAGE=Invalid Referral Link 
		FIELD LOG_NO= FIELD 
		LOG_MSG_NO=000000 
		FIELD MESSAGE_V1= 
		FIELD MESSAGE_V2= 
		FIELD MESSAGE_V3= 
		FIELD MESSAGE_V4= 
		FIELD PARAMETER= FIELD 
		ROW=0 
		FIELD FIELD= 
		FIELD SYSTEM=
	}


From above string how can I extract value of FIELD MESSAGE field.

What I have tried:

So far I have tried this...
C#
Message = function.GetValue("MESSAGE").ToString();
from = Message.IndexOf("FIELD MESSAGE=");
to = Message.LastIndexOf("FIELD LOG_NO=");
Msg = Message.Substring(from, to - from).Replace("FIELD MESSAGE=", "").Trim();


It is working fine. But, is there another way to do this. Like using LINQ, LIST, Lambda Expression, XML etc...
Posted
Updated 17-Jul-16 21:51pm
v3
Comments
John C Rayan 12-Jul-16 9:10am    
is the parameter FIELD LOG_NO always come after FIELD MESSAGE ?
sopi9 13-Jul-16 0:51am    
Yes.

So far I have tried this...
Message = function.GetValue("MESSAGE").ToString();
from = Message.IndexOf("FIELD MESSAGE=");
to = Message.LastIndexOf("FIELD LOG_NO=");
Msg = Message.Substring(from, to - from).Replace("FIELD MESSAGE=", "").Trim();

It is working fine. But, is there another way to do this. Like using LINQ, LIST, Lambda Expression, XML etc...

C#
var match = Regex.Match(Message, @"FIELD MESSAGE=(.+?)FIELD LOG_NO").Groups[1].Value;
 
Share this answer
 
v2
I'd try to parse such of data by using Linq:

C#
var data = msg.Replace("STRUCTURE ERRMSG", string.Empty)
            .Split(new string[]{"{", "}", "\n"}, StringSplitOptions.RemoveEmptyEntries)
            .Select(x=>new
                {
                    FieldName = x.Split('=')[0].Trim(),
                    FieldValue = x.Split('=').Count() == 1 ? string.Empty : x.Split('=')[1].Trim()
                })
            .ToList();


Above code returns:

FieldName     FieldValue
FIELD TYPE        E 
FIELD ID   
FIELD NUMBER      000 
FIELD MESSAGE     Invalid Referral Link 
FIELD LOG_NO      FIELD 
LOG_MSG_NO        000000 
FIELD MESSAGE_V1   
FIELD MESSAGE_V2   
FIELD MESSAGE_V3   
FIELD MESSAGE_V4   
FIELD PARAMETER   FIELD 
ROW               0 
FIELD FIELD   
FIELD SYSTEM


Note: i'd suggest to write custom class with Parse method to be able to deal with data.
 
Share this answer
 
C#
string fieldValue= Regex.Match(message, @"(?<=FIELD MESSAGE=)(.*\n?)(?=FIELD LOG_NO=)").Groups[1].Value;
 
Share this answer
 
Thanks for all for time and consideration. I am using below code

Message = function.GetValue("MESSAGE").ToString();
from = Message.IndexOf("FIELD MESSAGE=");
to = Message.LastIndexOf("FIELD LOG_NO=");
Msg = Message.Substring(from, to - from).Replace("FIELD MESSAGE=", "").Trim();
 
Share this answer
 

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