使用带有XmlAnyElementAttribute的服务引用代理时未声明XElement

时间:2010-10-07 02:07:11

标签: c# soap xml-serialization linq-to-xml windows-phone-7

我正在使用与第三方Soap服务对话的Windows Phone 7应用程序。我使用Add Service Reference到wsdl来生成代理类。有些通话有效,但使用“sObject”类的某些外拨通话会导致错误"The type System.Xml.Linq.XElement was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically"

定义了sObject类的相关部分(自动生成)

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:sobject.partner.soap.sforce.com")]
 public partial class sObject : object, System.ComponentModel.INotifyPropertyChanged {

    <snipped ....>

    [System.Xml.Serialization.XmlAnyElementAttribute(Namespace="urn:sobject.partner.soap.sforce.com", Order=3)]
    public System.Xml.Linq.XElement[] Any {
        get {
            return this.anyField;
        }
        set {
            this.anyField = value;
            this.RaisePropertyChanged("Any");
        }
    }

我用来创建sObject的代码:

sObject post = new sObject();
        XElement postEls = new XElement("sObject",
            new XElement("Type", "TextPost"),
            new XElement("ParentId", userInfo.userId),
            new XElement("Body", postBody)
            );

        post.Any = postEls.Elements().ToArray();

我尝试过的步骤:

[System.Xml.Serialization.XmlInclude(typeof(System.Xml.Linq.XElement))]添加到sObject类。同样的错误,似乎这应该有效。

[System.Xml.Serialization.XmlElement()]添加到Any属性。当我这样做时,消息被序列化了,但是 - 预期 - 不正确(Any元素不应该在那里,目标只是作为输出的XElements [])

 <sObject>
      <type xmlns="urn:sobject.partner.soap.sforce.com">FeedPost</type>
      <Id xsi:nil="true" xmlns="urn:sobject.partner.soap.sforce.com" />
      <Any xmlns="urn:sobject.partner.soap.sforce.com">
        <Type xmlns="">TextPost</Type>
      </Any>
      <Any xmlns="urn:sobject.partner.soap.sforce.com">
        <ParentId xmlns="">XXXXXX</ParentId>
      </Any>
      <Any xmlns="urn:sobject.partner.soap.sforce.com">
        <Body xmlns="">Test post from WP7!</Body>
      </Any>
    </sObject>

顺便说一下,我怀疑这是相关的,尽管RPC响应中存在数据,但是从第三方服务返回sObjects inbound的调用将其Any属性设置为null。

1 个答案:

答案 0 :(得分:1)

事实证明,异常是由于XElement []的序列化,目前是Windows Phone 7中的一个问题。

正如Vijay Verma here所述,关于XmlSerializer.Serialize状态的链接MSDN文章在Platform Notes下的状态:

  

Silverlight for Windows Phone:    如果使用包含XElement类型的对象数组的类型参数初始化XmlSerializer对象,则XmlSerializer.Serialize方法将抛出InvalidOperationException。

Vijay列出了使用单个XElement并分别解析字段的解决方法。如果您控制SOAP消息的两端,这将起作用。不幸的是,我没有这样的要求在第三方方面无效。

这解释了不期望带有XElement的InvalidOperationException,尽管不是我希望的答案。希望它可以帮助那些可能控制服务双方的其他人。

相关问题