为什么我的类型没有被 XmlSerializer 正确序列化 [英] Why is my Type not being Serialized correctly by the XmlSerializer

查看:26
本文介绍了为什么我的类型没有被 XmlSerializer 正确序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初的问题是,当我使用类型调用 webservice (asmx) 方法时,该类型总是通过 null .检查 Soap 确认该类型将作为一个空元素.所以我尝试了一个简单的测试.
这是我的类型,当然是从 WSDL 生成的

The intial problem was that when I called a webservice ( asmx) methos with a type the type was always going through as null . Inspecting the Soap confirmed that the type was going as an empty element. So I tried a simple test.
Here is my type which of course has been generated from WSDL

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://dto12.api.echosign")]
public partial class SendDocumentInteractiveOptions {

    private bool authoringRequestedField;

    private bool authoringRequestedFieldSpecified;

    private bool autoLoginUserField;

    private bool autoLoginUserFieldSpecified;

    private bool noChromeField;

    private bool noChromeFieldSpecified;

    /// <remarks/>
    public bool authoringRequested {
        get {
            return this.authoringRequestedField;
        }
        set {
            this.authoringRequestedField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool authoringRequestedSpecified {
        get {
            return this.authoringRequestedFieldSpecified;
        }
        set {
            this.authoringRequestedFieldSpecified = value;
        }
    }

    /// <remarks/>
    public bool autoLoginUser {
        get {
            return this.autoLoginUserField;
        }
        set {
            this.autoLoginUserField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool autoLoginUserSpecified {
        get {
            return this.autoLoginUserFieldSpecified;
        }
        set {
            this.autoLoginUserFieldSpecified = value;
        }
    }

    /// <remarks/>
    public bool noChrome {
        get {
            return this.noChromeField;
        }
        set {
            this.noChromeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool noChromeSpecified {
        get {
            return this.noChromeFieldSpecified;
        }
        set {
            this.noChromeFieldSpecified = value;
        }
    }
}

现在这里有一些简单的代码来序列化它.

Now here is some simple code to serialize it.

SendDocumentInteractiveOptions sdio = new SendDocumentInteractiveOptions();
sdio.authoringRequested = true;
sdio.autoLoginUser = true;
sdio.noChrome = true;
XmlSerializer xmlSer = new XmlSerializer(typeof(SendDocumentInteractiveOptions));
XmlWriter xw = new XmlTextWriter(@"g:\test.xml", null);
xmlSer.Serialize(xw, sdio);
xw.Close();  

这是生成的 XML

<?xml version="1.0"?&gt
&ltSendDocumentInteractiveOptions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

那么我在这里错过了什么.为什么我的公共属性没有被序列化?

So what am I missing here. Why are my public properties not getting serialized?

推荐答案

这是一个老问题,但很好.我认为解决方案可能在另一个问题 "为什么不是我的公共属性被 XmlSerializer 序列化了?".部分答案列出了属性不会被序列化的原因,并且在该列表中是

This is kind of an old question, but oh well. I think the solution might be in another question "Why isn't my public property serialized by the XmlSerializer?". Part of the answer lists reasons why an attribute would not be serialized and in that list is

  • 它有一个返回 false 的公共 bool FooSpecified {get;set;} 属性

在您的代码中,您设置了各种布尔值,但设置了相关的指定值.我遇到了类似的问题,并设置了指定的值为我修复了它.

In your code, you set the various Boolean values but set the related specified values. I was facing a similar issue and setting the specified value fixed it for me.

这篇关于为什么我的类型没有被 XmlSerializer 正确序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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