2012-11-06 45 views
3

這是我喜歡的類型:自定義XML序列化有了新的標籤和屬性和根

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

看起來您正嘗試使用自定義安全性標頭調用Web服務。通常,最簡單的方法是從目標Web服務的WSDL生成一組代理類。

要麼

  • 上使用添加在Visual Studio
  • 服務參考/添加Web引用;或者,如果您有服務的WSDL和XSD文件,然後使用wsdl.exe command line tool右擊(如wsdl.exe *.wsdl *.xsd //language:c#
  • here關於如何設置在ws:security header

安全信息,但是,如果100%肯定你需要獲得上面的確切soapEnv Xml,我建議你保持你的代碼'原樣'(即只需使用XmlSerializerDataContractSerializer將默認格式的MyObject序列化),然後使用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> 
+2

謝謝,真的healped – Saeid