Manipulating Message Fields
Adding Field
To associate a tag with a value, use the Set(int, string) method. If the field already exists, this method updates its value with the new value.
Note
It is also possible to set fields values using Message’s indexer.
There are Set(..)
methods for each standard type, e.g. Set(int, int) .
Such methods transform input value into text-presentation, so the stored value is a text-presentation of the input.
Accessing Field Value
To get a field value, use the Get(int) method.
Note
It is also possible to set fields values using Message
’s indexer.
Also, there are typed Get..
and TryGet..
methods, e.g GetInteger(int), TryGetInteger(int, out int).
These methods perform the conversion of the stored field value to the corresponding type.
Typed Get..
.. methods throw an exception if the conversion fails and typed TryGet..
methods return false
in such cases.
Removing Field
To remove a field, use the Remove(int) method.
GC-free Interface
The following methods do not allocate temporary string objects and gives an ability to manipulate fields without a GC pressure:
- CompareFieldValue(int, string)
- CompareType(string)
- CompareType(char)
- Set(int, StringBuilder)
- Get(int, StringBuilder)
This interface exists for Group, Message, SerializedMessage and FlatMessage classes.
Please see the "Using GC-free interface" article of the Low Latency Best Practices page for details.
Enumerating Fields
Message class supports the IEnumerable
interface,
therefore you can use a Message instance in the foreach
operator to enumerate message fields.
For example:
foreach (Field field in message)
{
Console.WriteLine($"{field.Tag}={field.Value}");
}
When you enumerate message fields, this does not include fields from repeating groups. To enumerate repeating group fields too take the corresponding GroupInstance instance from the repeating group, please see FIX Repeating Groups .
For example:
IterateOverFields(message, "");
void IterateOverFields(IFieldSet fieldSet, string messageLevel)
{
// Iterate over all fields of the message.
foreach (Field field in fieldSet)
{
Console.WriteLine(messageLevel + field.Tag.ToString() + "=" + field.Value);
if (fieldSet.TryGetGroup(field.Tag, out Group group))
{
messageLevel += " ";
Console.WriteLine(messageLevel + "Begin of group:");
IterateOverGroupInstances(group, messageLevel);
Console.WriteLine(messageLevel + "End of group");
}
}
}
void IterateOverGroupInstances(Group group, string messageLevel)
{
// Iterate over all group instances of the repeating group.
foreach (GroupInstance instance in group)
{
// Iterate over all fields of the group instance.
IterateOverFields(instance, messageLevel);
}
}
Named Constants
Each namespace that corresponds to a particular FIX version (e.g., OnixS.FIX.Fix44
) contains the Tag
class. This class contains constants for all-known tag numbers.
Additionally, there are classes that contains constants for all-known tag values.
The use of these constants makes the source code more readable.
For example:
Message order = new Message(MsgType.NewOrderSingle, ProtocolVersion.Fix44);
order.Set(Tag.ClOrdID, "90001008")
.Set(Tag.Side, Side.Buy)
.Set(Tag.TimeInForce, TimeInForce.Day);