Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF序列化不符合预期_C#_Asp.net_Wcf_Web Services_Xml Serialization - Fatal编程技术网

C# WCF序列化不符合预期

C# WCF序列化不符合预期,c#,asp.net,wcf,web-services,xml-serialization,C#,Asp.net,Wcf,Web Services,Xml Serialization,我有这样一个c#类作为我的WCF方法的回报: [Serializable] [XmlRoot("OutputItem")] public class MyItem { [XmlElement("ItemName")] public string NodeName { get; set; } [XmlArray("Fields"), XmlArrayItem(ElementName = "Field", Type = typeof(MyItemField))]

我有这样一个c#类作为我的WCF方法的回报:

[Serializable]
[XmlRoot("OutputItem")]
public class MyItem
{
    [XmlElement("ItemName")] 
    public string NodeName { get; set; }

    [XmlArray("Fields"), XmlArrayItem(ElementName = "Field", Type = typeof(MyItemField))]
    public List<MyItemField> Fields { get; set; }

}
我希望此文件的XML输出如下:

<xml version="1.0" encoding="utf-16"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <GetItemXMLResponse xmlns="http://www.here.com/XML/ItemService.xsd">
      <GetItemXMLResult>
        <OutputItem>
           <ItemName>FR</ItemName>
           <Fields>
            ......
           </Fields>
        </OutputItem>
      </GetItemXMLResult>
    </GetItemXMLResponse>
  </s:Body>
</s:Envelope>

FR
......
但是,输出如下-顶部没有
指令:

<xml version="1.0" encoding="utf-16"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <GetItemXMLResponse xmlns="http://www.here.com/XML/ItemService.xsd">
      <GetItemXMLResult>
           <ItemName>FR</ItemName>
           <Fields>
            ......
           </Fields>
      </GetItemXMLResult>
    </GetItemXMLResponse>
  </s:Body>
</s:Envelope>

FR
......

我遗漏了什么?

如果我没记错的话,这完全取决于您的[运营合同]是如何定义的。您可能必须使用消息契约来获得所需的行为。看看模型对象 [可序列化] [XmlRoot(“输出项”)] [DataContractAttribute] 公共类MyObject { [XmlElement(“ItemName”)] [数据成员属性] 公共字符串名称{get;set;} [XmlArray(“DummyItems”)] [XmlArrayItem(“DummyItem”,typeof(MyItemField))] 公共列表DummyItem{get;set;} } //实现契约的类 [数据合同] 公共类消费服务:IAnyContract { 公共MyObject GetItemXML(字符串id) { MyObject mo=新的MyObject(); //做一些事情来填充mi 肌体mo; } }
我不知道确切的区别,但是当从XSD或WSDL生成代码时,它使用“XmlRootAttribute”(“InsertYourNodeName”)而不是“XmlRoot”(“InsertYourNodeName”)。我很确定其中一个继承了另一个,但您可能想尝试用XmlRootAttribute替换它,看看是否有不同之处。我认为您必须用[DataContract]来修饰类型。还有带有[DataMember]的成员,但它们似乎无论如何都会被反序列化。也许我把模型和合同搞混了。
<xml version="1.0" encoding="utf-16"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <GetItemXMLResponse xmlns="http://www.here.com/XML/ItemService.xsd">
      <GetItemXMLResult>
           <ItemName>FR</ItemName>
           <Fields>
            ......
           </Fields>
      </GetItemXMLResult>
    </GetItemXMLResponse>
  </s:Body>
</s:Envelope>
// The Model Object

[Serializable]
[XmlRoot("OutputItem")]
[DataContractAttribute]
public class MyObject
{
    [XmlElement("ItemName")]
    [DataMemberAttribute] 
    public string Name { get; set; }

    [XmlArray("DummyItems")]
    [XmlArrayItem("DummyItem", typeof(MyItemField))]
    public List<Fields> DummyItem { get; set; }
}



// The Class that implement the contract
[DataContract]
public class ConsumptionService : IAnyContract
{
    public MyObject GetItemXML(string id)
    {
       MyObject mo = new MyObject();
       //do some stuff to populate mi
       MyObject mo;   
    }
 }