1

我有两个类,一个实现IXmlSerializable,一个具有DataContract属性:

public class Foo : IXmlSerializable
{
    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        reader.MoveToContent();
        XElement element = (XElement)XNode.ReadFrom(reader);

        if (element.Element("Foo") != null)
            element = element.Element("Foo");

        Property = Convert.ToInt32(element.Element("Property").Value);
    }

    public void WriteXml(XmlWriter writer)
    {
        var element = new XElement("Foo");
        element.Add(new XElement("Property", Property));

        element.WriteTo(writer);
    }

    public int Property { get; set; }
}

[DataContract]
public class Bar
{
    [DataMember]
    public int Property { get; set; }
}

然后我有服务接口

[ServiceContract]
public interface IFooBarService
{
    [OperationContract]
    void TestFoo(Foo toTest);

    [OperationContract]
    void TestListFoo(Foo[] toTest);

    [OperationContract]
    void TestBar(Bar toTest);

    [OperationContract]
    void TestListBar(Bar[] toTest);
}

并将其实现为:

public class FooBarService : IFooBarService
{

    public void TestFoo(Foo toTest)
    {
        var a = toTest.Property;
    }

    public void TestListFoo(Foo[] toTest)
    {
        foreach (var item in toTest)
        {
            var x = item.Property;
        }
    }

    public void TestBar(Bar toTest)
    {
        var a = toTest.Property;
    }

    public void TestListBar(Bar[] toTest)
    {
        foreach (var item in toTest)
        {
            var x = item.Property;
        }
    }
}

SOAP UI 生成调用服务所需的 xml。TestListFoo除了对我收到空数组的调用之外,一切正常

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:com="http://schemas.datacontract.org/2004/07/Com.Panotec.Remote.Core.WebService" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <soapenv:Header/>
   <soapenv:Body>
     <tem:TestListFoo>
       <tem:toTest>      
         <Foo>
           <Property>1</Property>
         </Foo>    
         <Foo>
           <Property>1</Property>
         </Foo>    
         <Foo>
           <Property>1</Property>
         </Foo>    
         <Foo>
           <Property>1</Property>
         </Foo>
       </tem:toTest>
     </tem:TestListFoo>
   </soapenv:Body>
</soapenv:Envelope>

我错过了什么?是否有可能实现我所需要的?如果没有,如何将DataContract属性添加到实现的类中IXmlSerializable

谢谢

4

0 回答 0