3

这是我的类型:

public class MyObject {

    public string destAdd { get; set; }
    public long Time { get; set; }
    public int maxNumb { get; set; }
    public Account AccountCredentials { get; set; }

    public System.String Serialize() {
        String result = "";
        XmlSerializer xs = new XmlSerializer(typeof(MyObject));
        MemoryStream ms = new MemoryStream();
        xs.Serialize(ms, this);
        result = System.Text.Encoding.UTF8.GetString(ms.ToArray());
        ms.Close();
        ms.Dispose();
        xs = null;
        return result;
    }

    public static MyObject DeSerialize(String s) {
        MyObject result = new MyObject();
        XmlSerializer xs = new XmlSerializer(typeof(MyObject));
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(s));
        result = (MyObject)xs.Deserialize(ms);
        ms.Close();
        ms.Dispose();
        xs = null;
        return result;
    }
}

然后我像这样序列化它:

        MyObject obj = new MyObject();
        obj.destAdd = "Destination";
        obj.maxNumb = 99;
        obj.Time = 128;
        obj.Account = new Account { username = "user", password = "pass" };
        string seializeObj = obj.Serialize();

结果是:

<?xml version="1.0"?>
<MyObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <destAdd>Destination</destAdd>
  <Time>128</Time>
  <maxNumb>99</maxNumb>
  <Account>
    <username>user</username>
    <password>pass</password>
  </Account>
</MyObject>

但我需要以下结果:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:smag="http://targetaddress.com/">
  <soapenv:Header>
    <Account>
      <username>user</username>
      <password>pass</password>
    </Account>
  </soapenv:Header>
  <soapenv:Body>
    <smag:myobjinfos>
      <destAdd>Destination</destAdd>
      <Time>128</Time>
      <maxNumb>99</maxNumb>
    </smag:myobjinfos>
  </soapenv:Body>
</soapenv:Envelope>

如何实现序列化以获得此结果?有什么建议吗?

4

1 回答 1

4

您似乎正在尝试使用自定义安全标头调用 Web 服务。通常,最简单的方法是从目标 Web 服务的 WSDL 生成一组代理类。

任何一个

但是,如果您 100% 确定您需要获得上述准确soapEnv Xml信息,我建议您保持代码“原样”(即,使用XmlSerializeror将 MyObject 序列化为其默认格式DataContractSerializer),然后使用XslCompiledTransform

这个 XSLT 将完全做到这一点:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:template match="/MyObject">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                          xmlns:smag="http://targetaddress.com/">
            <soapenv:Header>
                <Account>
                    <username><xsl:value-of select="Account/username"/></username>
                    <password><xsl:value-of select="Account/password"/></password>
                </Account>
            </soapenv:Header>
            <soapenv:Body>
                <smag:myobjinfos>
                    <destAdd><xsl:value-of select="destAdd"/></destAdd>
                    <Time><xsl:value-of select="Time"/></Time>
                    <maxNumb><xsl:value-of select="maxNumb"/></maxNumb>
                </smag:myobjinfos>
            </soapenv:Body>
        </soapenv:Envelope> </xsl:template>
</xsl:stylesheet>

转换

<?xml version="1.0"?>
<MyObject>
  <destAdd>Destination</destAdd>
  <Time>128</Time>
  <maxNumb>99</maxNumb>
  <Account>
    <username>user</username>
    <password>pass</password>
  </Account>
</MyObject>

对此:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:smag="http://targetaddress.com/">
  <soapenv:Header>
    <Account>
      <username>user</username>
      <password>pass</password>
    </Account>
  </soapenv:Header>
  <soapenv:Body>
    <smag:myobjinfos>
      <destAdd>Destination</destAdd>
      <Time>128</Time>
      <maxNumb>99</maxNumb>
    </smag:myobjinfos>
  </soapenv:Body>
</soapenv:Envelope>
于 2012-11-06T12:54:26.233 回答