【问题标题】:How can I convert or display a XML format message to SOAP envelop format in C#.?如何在 C# 中将 XML 格式的消息转换或显示为 SOAP 信封格式。?
【发布时间】:2017-02-09 07:56:29
【问题描述】:

我有一个 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# 中完成。

【问题讨论】:

  • 嗯 ...不确定您到底在问什么,但是如果您想将 XML 从顶部转换为底部的 XML ...只需添加缺少的文本(作为字符串文字或使用 XmlWriter)。

标签: c# xml soap


【解决方案1】:

尝试以下:

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 
            });
        }
    }
}

【讨论】:

  • 我得到空白输出,我应该在哪里看到 SOAP 格式的输出。
  • xml 在信封中。你可以得到一个字符串如下: str envelopeStr = envelope.ToString()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 2015-07-25
  • 1970-01-01
  • 2017-12-12
  • 2016-01-07
  • 1970-01-01
  • 2017-11-22
相关资源
最近更新 更多