[英]Populating XmlAnyAttribute in WCF Client
我有一个为SOAP服务创建的WCF客户端,其架构中包含以下类型:
<xs:element name="foo" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="bar" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute namespace="##other"/>
</xs:complexType>
</xs:element>
.NET生成以下类:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1098.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://example.com/ws/entity")]
public partial class foo : object {
private string barField;
private System.Xml.XmlAttribute[] anyAttrField;
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public string bar {
get { return this.barField; }
set { this.barField = value; }
}
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public System.Xml.XmlAttribute[] AnyAttr {
get { return this.anyAttrField; }
set { this.anyAttrField = value; }
}
}
在此类上填充AnyAttr属性的推荐方法是什么? 我可以找到创建XmlAttribute
的唯一方法是来自现有的XmlDocument
,在这种情况下,直到对象在出路时被序列化才会创建。
我开始尝试在BeforeSendRequest
中向IClientMessageInspector
的元素添加一个属性,但是我找到修改原始XmlDocument
的唯一方法是创建Message
的副本,转换它,然后替换原始消息,这似乎是一个很多工作只是为了添加一个属性。
最终,我正在尝试创建看起来像这样的XML:
...
<foo xmlns:ns2="http://example.com/ws/ext" ns2:other="some other value">
<bar>some value</bar>
</foo>
...
我确实拥有定义Foo
的服务,虽然我无法修改Foo
本身,但我能够将自定义元素添加到模式中。 因此,我没有尝试模式中现有的xs:anyAttribute
,而是为我的数据添加了一个新元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.