.net - XmlTypeAttribute 不更改类型名称

标签 .net xml-serialization

我正在开发一个使用 WSE3 调用 Web 服务的项目。最初使用 VS2005 生成的类型已随着时间的推移进行了修改。现在,我需要更改 SOAP 消息中的类型名称。我认为应该使用 XmlTypeAttribute 来完成,但这不会影响类型名称。作为实验,我在该类的属性上使用了 XmlElementAttribute,这确实更改了为该属性生成的元素的名称。生成的对象已使用部分类进行了扩展。

SOAP 类型作为“地址”通过网络传输。我不确定为什么 XmlTypeAttribute 不影响它,或者为什么它会遇到小写。

关于我可能做错了什么的想法,或者实现目标的更好方法?

引用文献.cs:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.1434")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "MyAddress", Namespace = "http://sample.com/transaction")]
//                                                     ^-- Soap typenamed "address", not "MyAddress"
public partial class Address
{        
    private string address1Field;

    private string address2Field;

    private string[] jurisdictionsField;

    private System.DateTime resolvedDateField;

    private bool resolvedDateFieldSpecified;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("MyAddress1", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    //                                             ^--- SOAP element named "MyAddress1" as expected
    public virtual string Address1
    {
        get {
            return this.address1Field;
        }
        set {
            this.address1Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("address2", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public virtual string Address2
    {
        get {
            return this.address2Field;
        }
        set {
            this.address2Field = value;
        }
    }        
}

地址.cs:

public partial class Address
{
    private int id;

    public virtual int Id
    {
        get { return id; }
        set { id = value; }
    }
}

最佳答案

[XmlType] 更改架构中 complexType 的名称。它不会更改 XML 中的元素名称。

改用[XmlElement(ElementName="MyAddress", Namespace="your namespace")]

关于.net - XmlTypeAttribute 不更改类型名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1579982/

相关文章:

c# - 如何使用 C# XML 序列化反序列化以下文件,其中根本身是 IEnumerable?

c# - 有没有办法通过反射或其他方式获取实体 ID 字段的名称?

c# - FormView.DataItem 在回发时为 null

c# - ASP.NET MVC : The model item passed into the dictionary is of type 'ClassA' , 但此字典需要类型为 'ClassA' 的模型项

c# - 转换 XML 以使用较新版本进行反序列化

java - 简单的Xml列表解析问题

.net - GDI+实际上还是 "usable"技术吗?

.NET 客户端崩溃时将 COM 注册的 .dll 替换为与客户端相同的 .NET 版本中的新 .dll

.net - 继承类时使用 XMLTextAttribute

c# - 如何序列化List<IFilter>(诺基亚成像SDK)?