2

更新:2012 年 11 月 26 日我已经使用 wsdl.exe 而不是 svcutil 更新了我的自动生成的 c# 类,因此我可以在代理类中获取 SOAP 属性(我正在调用 SOAP 服务)。我最初的问题是序列化,但在 markus 的帮助下,我现在可以序列化了。但现在我从服务中得到一个错误:

无效的请求对象:它必须是一个查询请求

当我对其进行硬编码时起作用的xml:

<xml version="1.0"?><Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Header/><Body>
<QueryRequest xmlns="http://emkt.pjm.com/emkt/xml">
<QueryMarketResults type="Virtual" day="2012-11-16"><All/>
</QueryMarketResults></QueryRequest></Body></Envelope>

它生成的xml是:

<?xml version="1.0" encoding="utf-16"?>
<QueryRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://emkt.pjm.com/emkt/xml">
  <QueryMarketResults day="2012-11-16" type="Virtual">
    <All />
  </QueryMarketResults>
</QueryRequest>

我缺少信封和身体标签。我尝试如下序列化,但收到错误“生成 XML 文档时出错”。

XmlTypeMapping myTypeMapping = (new SoapReflectionImporter().ImportTypeMapping(typeof (QueryRequest)));
XmlSerializer serializer = new XmlSerializer(myTypeMapping);

以下是相关的代理类:(我在 QueryRequest 下面添加了 [XmlRoot] 属性,它确实添加了正确的命名空间)

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=false, Namespace="http://emkt.pjm.com/emkt/xml")]
[XmlRoot(Namespace = "http://emkt.pjm.com/emkt/xml")]
public partial class QueryRequest {

    private object[] itemsField;

    private ItemsChoiceType1[] itemsElementNameField;

    [System.Xml.Serialization.XmlElementAttribute("QueryPortfolios", typeof(QueryPortfoliosType))]
    [System.Xml.Serialization.XmlElementAttribute("QueryVirtualBid", typeof(QueryByAllLocationDayType))]
    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public ItemsChoiceType1[] ItemsElementName {
        get {
            return this.itemsElementNameField;
        }
        set {
            this.itemsElementNameField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://emkt.pjm.com/emkt/xml", IncludeInSchema=false)]
public enum ItemsChoiceType1 {
    QueryPortfolios,
    QueryVirtualBid,
}

感谢你的帮助!

4

1 回答 1

0

构造函数不会为数组分配任何值,因此它们将为空。

尝试这个:

var qr = new QueryRequest
{
    ItemsElementName = new ItemsChoiceType1[] {
        ItemsChoiceType1.QueryMarketResults,
    },
    Items = new object[] {
        new QueryByAllLocationDayTypeType
        {
            ItemElementName = ItemChoiceType3.All,
            Item = new QueryAllType(),
            day = Convert.ToDateTime("2012-11-16"),
            type = MarketQueryTypeType.Virtual,
        },
    },
}

或者,等效地没有对象初始化器:

var qmr = new QueryByAllLocationDayTypeType();
qmr.ItemElementName = ItemChoiceType3.All;
qmr.Item = new QueryAllType();
qmr.day = Convert.ToDateTime("2012-11-16");
qmr.type = MarketQueryTypeType.Virtual;

var qr = new QueryRequest();
qr.ItemsElementName = new ItemsChoiceType1[1];
qr.ItemElementName[0] = ItemsChoiceType1.QueryMarketResults;
qr.Items = new object[1];
qr.Items[0] = qmr;
于 2012-11-20T06:02:08.240 回答