This is my Type:
public class MyObject {
public string destAdd { get; set; }
public long Time { get; set; }
public int maxNumb { get; set; }
It appears that you are trying to call a web service, with a custom security header. Usually, the easiest way to do this would be to generate a set of proxy classes from the WSDL of the target webservice.
Either
wsdl.exe *.wsdl *.xsd //language:c#
)However, if you are 100% sure that you need to obtain the exact soapEnv Xml
above, I would suggest you keep your code 'as is' (i.e. just serialize MyObject in its default format using XmlSerializer
or DataContractSerializer
), and then use a XslCompiledTransform.
This XSLT will do exactly this:
<?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>
Converts
<?xml version="1.0"?>
<MyObject>
<destAdd>Destination</destAdd>
<Time>128</Time>
<maxNumb>99</maxNumb>
<Account>
<username>user</username>
<password>pass</password>
</Account>
</MyObject>
To this:
<?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>