自定义XML序列化有了新的标签和属性和根 [英] Customize XML Serialize With new Tags And Attributes And Root

查看:121
本文介绍了自定义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;
    }
}

然后我序列化是这样的:

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();

的结果是:

<?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?

推荐答案

看来,你试图调用Web服务,以自定义的安全标头。通常情况下,要做到这一点最简单的方法是将产生从目标Web服务的WSDL一组代理类的。

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.


  • 右键点击使用添加服务引用/从Visual Studio
  • 添加Web引用
  • 或者,如果你有服务的WSDL和XSD文件,然后使用的 Wsdl.exe用命令行工具(如 Wsdl.exe用* *的.wsdl //名为.xsd语言:C#

  • 见<一href=\"http://stackoverflow.com/questions/2139190/adding-soapheader-username-and-password-with-wse-3-0\">here关于如何设置在 WS安全信息:安全头

  • 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

不过,如果你是100%肯定,你需要获取准确的 soapEnv XML 上面,我建议你保持你的code'是'(即只使用序列为MyObject在其默认格式为:的XmlSerializer 的DataContractSerializer ),然后用<一个href=\"http://stackoverflow.com/questions/34093/how-to-apply-an-xslt-stylesheet-in-c-sharp%20to%20the%20result\">XslCompiledTransform.

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.

这个XSLT会做正是这一点:

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>

将转换

<?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>

这篇关于自定义XML序列化有了新的标签和属性和根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆