如何在C#中将XML格式消息转换或显示为SOAP信封格式?

时间:2017-02-09 07:56:29

标签: c# xml soap

我有一个FIX(财务信息交换)消息格式,例如35=U1 49=GEMI1 8=FIX.4.1 9=7328=FIX.4.1 9=751 35=U1 34=3 49=GEMI2 52=20160125-10:52:21,我转换为XML格式。

<messageTags>
<tag key="9" value="751" />
<tag key="8" value="FIX.4.1" />
<tag key="35" value="U1" />
<tag key="34" value="3" />
<tag key="49" value="GEMI2" />
<tag key="52" value="20160125-10:52:21" />
</messageTags>

我想以下面的SOAP格式转换或显示这种类型的xml。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eu="http://client.ws.emx.co.uk">
 <soapenv:Header></soapenv:Header>
 <soapenv:Body>
<eu:processXmlMessageRequest>
<sequenceNumber>1</sequenceNumber>
<creationTime>2016-05-28T09:36:22.165</creationTime>
<messageTags>
        <tag key="8" value="FIX.4.1" />
        <tag key="9" value="751" />
        <tag key="35" value="U1" />
        <tag key="34" value="3" />
        <tag key="49" value="GEMI2" />
        <tag key="52" value="20160125-10:52:21" />
</messageTags>
<payloadType>XmlFixMessageTags</payloadType>
</eu:processXmlMessageRequest>
 </soapenv:Body>
</soapenv:Envelope>

任何人都可以提供帮助,如何在Visual Studio C#中执行此操作。

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XElement messageTags = XElement.Load(FILENAME);

            string header =
                "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:eu=\"http://client.ws.emx.co.uk\">" +
                     "<soapenv:Header></soapenv:Header>" +
                     "<soapenv:Body>" +
                       "<eu:processXmlMessageRequest>" +
                       "</eu:processXmlMessageRequest>" +
                     "</soapenv:Body>" +
                "</soapenv:Envelope>";

            XElement envelope = XElement.Parse(header);
            XElement request = envelope.Descendants().Where(x => x.Name.LocalName == "processXmlMessageRequest").FirstOrDefault();

            int sequenceNumber = 1;
            DateTime now = DateTime.Now;
            request.Add(new object[] {
               new XElement("sequenceNumber", sequenceNumber),
               new XElement("creationTime", now.ToString("yyyy-MM-ddT" + now.TimeOfDay.ToString())), //>2016-05-28T09:36:22.165</creationTime>
               messageTags 
            });
        }
    }
}