This is my Type:
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;
}
}
Then I serialize it like this:
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();
The result is:
<?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>
But I need the following result:
<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>
How can I implement the serialize to get this result? any suggestion?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…