Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

c# - Customize XML Serialize With new Tags And Attributes And Root

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  • Right click on the use Add Service Reference / Add Web Reference from the Visual Studio
  • Or, if you have the WSDL and xsd files of the service, then use wsdl.exe command line tool (e.g. wsdl.exe *.wsdl *.xsd //language:c#)
  • See here on how to set security information on the ws:security header

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...