【发布时间】:2017-02-15 14:20:48
【问题描述】:
我有以下FIX格式的输入(可能会根据需要改变)。我想在 SOAP 信封中使用 XML 格式。我在 C# 中使用了 SoapFormatter 类,但得到了错误的输出。
输入:35=U149=GEMI18=FIX.4.19=73256=EMX
button_click() {
Dictionary<int, string> rawMessagedictionary = richTextBox1.Text.Split('\u0001').Select(part => part.Split('=')).Where(part => part.Length == 2).ToDictionary(sp => Convert.ToInt32(sp[0]), sp => sp[1]);
richTextBox2.Text = DictToXml(rawMessagedictionary, "messageTags", "tag").ToString();
string soap= ObjectToSoapMessage(richTextBox2.Text);
MessageBox.Show(soap);
}
// Method to Convert object message to SOAP format.
public static string ObjectToSoapMessage(object messageString)
{
using (MemoryStream Stream = new MemoryStream())
{
SoapFormatter serializer = new SoapFormatter();
serializer.Serialize(Stream, messageString);
Stream.Flush();
return UTF8Encoding.UTF8.GetString(Stream.GetBuffer(), 0, (int)Stream.Position);
}
}
// Code to convert FIX message to XML simple format
public static XElement DictToXml
(Dictionary<int, string> inputDict, string elmName, string valuesName)
{
XElement outElm = new XElement(elmName);
Dictionary<int, string>.KeyCollection keys = inputDict.Keys;
foreach (int key in keys)
{
XElement inner = new XElement(valuesName);
inner.Add(new XAttribute("key", key));
inner.Add(new XAttribute("value", inputDict[key]));
outElm.Add(inner);
}
return outElm;
}
输出如下:
<messageTags>
<tag key="35" value="U1" />
<tag key="49" value="GEMI1" />
<tag key="8" value="FIX.4.1" />
<tag key="9" value="732" />
<tag key="56" value="EMX" />
</messageTags>
// 我在
中得到错误的输出 string soap= ObjectToSoapMessage(richTextBox2.Text);
MessageBox.Show(soap);
如下:
<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<SOAP-ENC:string id="ref-1"><messageTags>
<tag key="35" value="U1" />
<tag key="49" value="GEMI1" />
<tag key="8" value="FIX.4.1" />
<tag key="9" value="732" />
<tag key="56" value="EMX" />
</messageTags></SOAP-ENC:string>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
为什么?输出是这种格式..?
【问题讨论】:
-
ObjectToSoapMessage() 给我错误的输出如下: