In my application I have a PacketList
class and Packet
class. I'd like to be able to use the serialization helpers on PacketList
to come up with something like the desired output, but have no idea what direction to go in.
I am writing a program to imitate a server which has a peculiar protocol for sending data.
Client sends data with format: COMMAND|ARGUMENT_0|ARGUMENT_1|ARGUMENT_2|...|ARGUMENT_N\0
. Where COMMAND
could be something like MOVE
or LOGIN
.
The server would respond in the format:
<p c='COUNT'>
<m p='N' p0='COMMAND_0' p1='ARUGMENT_0' ... pN='ARGUMENT_N'/>
<m p='N' p0='COMMAND_1' p1='ARUGMENT_0' ... pN='ARGUMENT_N'/>
<m p='N' p0='COMMAND_2' p1='ARUGMENT_0' ... pN='ARGUMENT_N'/>
<m p='N' p0='COMMAND_3' p1='ARUGMENT_0' ... pN='ARGUMENT_N'/>
...
<m p='N' p0='COMMAND_COUNT' p1='ARUGMENT_0' ... pN='ARGUMENT_N'/>
</p>
Where COMMAND_0
could be something like UPDATE_POSITION
or AUTHENTICATED
.
Yes, this is a silly way of doing things. No, I don't know why it's done this way. No, I can't change it.
Anyways, I am looking to emulate the way that the server sends the packet back to the client. What I have gotten so far is:
XmlWriterSettings _Settings = new XmlWriterSettings {
OmitXmlDeclaration = true,
Indent = true
};
StringBuilder _Xml = new StringBuilder();
XmlWriter _Writer = XmlWriter.Create(_Xml, _Settings);
_Writer.WriteStartElement("p");
_Writer.WriteAttributeString("c", "1");
_Writer.WriteStartElement("m");
_Writer.WriteAttributeString("p", "2");
_Writer.WriteAttributeString("p0", "COMMAND");
_Writer.WriteAttributeString("p1", "ARGUMENT_0");
_Writer.WriteAttributeString("p2", "ARGUMENT_1");
_Writer.WriteEndElement(); // </m>
_Writer.WriteEndElement(); // </p>
_Writer.Flush();
Console.WriteLine(_Xml.ToString());
This works properly and outputs:
<p c="1">
<m p="2" p0="COMMAND" p1="ARGUMENT_0" p2="ARGUMENT_1" />
</p>
However, I'd like to implement this in a cleaner way.
My PacketList
basically contains a list of Packet
s, and Packet
contains a String _Command
and String[] _Arguments
.
If anyone could guide me in the right direction it would be much appreciated.