I have a C# winforms application. In this application I am creating a list of CanMessage
objects dynamically from an xml file.
The xml file is constructed as below
<message id="0x641" ecu="BCM" name="BODY9" dlc="8" cyclicrate="500">
<bytes>
<byte0>0x0</byte0>
<byte1>0x0</byte1>
<byte2>0x0</byte2>
<byte3>0x0</byte3>
<byte4>0x0</byte4>
<byte5>0x0</byte5>
<byte6>0x0</byte6>
<byte7>0x0</byte7>
</bytes>
The Canmessage
object is constructed like this:
CanMessage(String name,String _EcuName, ulong id, ushort dlc,int[] bytearray , int cyclic)
{
this.Name = name;
this.EcuName = _EcuName;
this.Id = id;
this.Dlc = dlc;
this.Bytes = new int[dlc];
this.CyclicRate = cyclic;
int i = 0;
for(i = 0; i < dlc; i++)
{
this.Bytes[i] = bytearray[i];
}
}
Below is how I am building my Canmessage
list:
public void BuildCanList()
{
try
{
XmlDocument xd = new XmlDocument();
xd.Load(XmlFile);
XmlNodeList nodelist = xd.SelectNodes("/messages/message");
foreach (XmlNode n in nodelist)
{
String name, ecuname;
ulong id;
ushort dlc;
int[] bytes = new int[Convert.ToInt32(n.Attributes.GetNamedItem("dlc").Value)];
int cyclic;
name = n.Attributes.GetNamedItem("name").Value.ToString();
id = (ulong)Convert.ToInt32(n.Attributes.GetNamedItem("id").Value, 16);
ecuname = n.Attributes.GetNamedItem("ecu").Value.ToString();
dlc = (ushort)Convert.ToByte(n.Attributes.GetNamedItem("dlc").Value);
cyclic = Convert.ToInt32(n.Attributes.GetNamedItem("cyclicrate").Value);
XmlNode sn = n.SelectSingleNode("bytes");
for (ushort i = 0; i < dlc; i++)
{
try
{
bytes[i] = Convert.ToInt32(sn.ChildNodes.Item(i).InnerText, 16);
}
catch(Exception e)
{
bytes[i] = 0x0;
Console.WriteLine(String.Format("Error Building can Message: {0}", e.ToString()));
}
}
CanMessage cm = new CanMessage(name, ecuname, id, dlc, bytes, cyclic);
CanList.Add(cm);
}
My list is being created with no issues. My question is after my list has been created I will sometimes need to do some bit manipulation on certain bytes of a certain Canmessage
. How can I select a message from the list based on its name
property and then edit certain bytes from that message? I know how to select a message from the list using lambda expression and linq. But I don't know how to then combine that select method with an edit and save method or if this is even the best way to go about doing this.