# What is the best way to parse a Fix Protocol message in C#

#### mottapd

Hi, good morning.

I'm trying to find a best way(with the best performance) to parse a Fix Protocol message.
Then I made some functions that return a value if I give a Fix Message and a Fix field. They work pretty well but I don't know if is the best way to do this.
See below:
C++:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FixField
{
class Program
{
private const char cEql = (char)61;
private const char cFixSep = (char)1;
private const string cFixMsg = @"8=FIX.4.29=27735=834=2489149=ABCD52=20100415-16:01:06.98256=FIX0999991=222222226=011=00500002000000097814=017=0101069762390120=031=032=037=2010041500578358587538=100039=040=241=00500002000000097744=000026.73054=255=IBM59=060=20100415-16:01:07150=0151=00000000100010=148";

static void Main(string[] args)
{
Console.WriteLine(FixField(cFixMsg,"9"));
Console.WriteLine(FixField2(cFixMsg, "9"));
}

static string FixField(string strFixMsg, string strField)
{
int intPos;
int intFieldLen;
int intEnd;
string strValue;

intFieldLen = strField.Length + 2;

intPos = strFixMsg.IndexOf(cFixSep + strField + cEql);

intEnd = strFixMsg.IndexOf(cFixSep, intPos + 1);

if (intPos == -1)
{
strValue = strFixMsg.Substring(2, intEnd - 2);
}
else
{
strValue = strFixMsg.Substring(intPos + intFieldLen, intEnd - intPos - intFieldLen);
}

return strValue;
}

static string FixField2(string strFixMsg, string strField)
{
string[] strValue = strFixMsg.Split(new char[] { cFixSep, cEql });

int i;

for (i = 0; strValue[I] != strField; i++)[/I]
{ }

return strValue[i + 1];
}

}
}
best regards,

bruno

#### JKPappas

##### GPUs-R-US
Have you looked around for any open-source .NET libraries for working with FIX? E.g., http://www.codeproject.com/KB/cs/fixprotocol.aspx

If you're only parsing one kind of message (what you posted above) and your code already works, then I wouldn't over-engineer the problem. I'm not very familiar with the FIX message format, but you might be able to parse it with a Regex as well (though if you do, test the performance against your existing code to see which is faster).

Replies
39
Views
156K
Replies
15
Views
20K
Replies
7
Views
36K
Replies
8
Views
14K
Replies
83
Views
89K