Optional XML elements for web service SOAP contract

Aaron Scribner <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <[email protected]>
I have run into a snag with my web service and not finding an easy way
around this one. We have the following object setup and is current used as
the input parameter for the web method.



      Order <<- OrderLines <<- OrderLineMarkings <<-
OrderLineMarkingVariables.

      <<- used to denote a many-to-one relationship.



The problem lies with the client usage of one of our SKU's that does not
contain any user-defined variables for the order, thus making the
OrderLineMarking element of this OrderLine null and causing the following
error.



Error 144   TestCase 'UnitTests.WebServiceTests.InsertOrder5'

failed: System.Web.Services.Protocols.SoapException :
System.Web.Services.Protocols.SoapException: Server was unable to read
request. ---> System.InvalidOperationException: There is an error in XML
document (1, 1575). ---> System.NullReferenceException: Object reference not
set to an instance of an object.

   at
WcfOrderEntry.OrderEntryASP.OrderComponents.OrderLine.set_OrderLineMarking(OrderLineMarking[]
value) in
D:\Projects\WcfOrderEntry\WIP\OrderEntryASP\OrderComponents\OrderLine.cs:line
107

   at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read4_OrderLine(Boolean
isNullable, Boolean checkType)

   at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read5_Order(Boolean
isNullable, Boolean checkType)

   at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read6_InsertNewOrder()

   at
Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer.Deserialize(XmlSerializationReader
reader)

   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle, XmlDeserializationEvents events)

   --- End of inner exception stack trace ---

   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle, XmlDeserializationEvents events)

   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle)

   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()

   --- End of inner exception stack trace ---

   at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()

   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

   at
System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage
message, WebResponse response, Stream responseStream, Boolean asyncCall)

   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String
methodName, Object[] parameters)

   at
OrderEntryTestApplication.localhost.OrderEntryAspWebService.InsertNewOrder(Order
order) in D:\Projects\WcfOrderEntry\WIP\OrderEntryTestApplication\Web
References\localhost\Reference.cs:line 83

   at UnitTests.WebServiceTests.InsertOrder5() in
D:\Projects\WcfOrderEntry\WIP\OrderEntryTestApplication\UnitTests\OrderEntryTests.cs:line
249      D:\Projects\WcfOrderEntry\WIP\OrderEntryTestApplication\Web
References\localhost\Reference.cs   83



A valid OrderLine element looks like



  <OrderLine xmlns="http://removed.com/">

    <SkuName>FDX-840/GTXF-B-SET1</SkuName>

    <Quantity>999</Quantity>

    <Comment>339</Comment>

    <OrderLineMarking>

      <NumberOfSets>1</NumberOfSets>

      <Template>840OFPGTXF_S1</Template>

      <OrderLineMarkingVariable>

        <Position>0</Position>

        <Name>ManagementNumber_5_Digit</Name>

        <Value>001_999</Value>

        <IsStatic>false</IsStatic>

      </OrderLineMarkingVariable>

    </OrderLineMarking>

  </OrderLine>



The OrderLine element causing problems is



  <OrderLine xmlns="http://removed.com/">

    <SkuName>USDA840FDX/GESM1-Y-4</SkuName>

    <Quantity>10</Quantity>

    <Comment>339</Comment>

  </OrderLine>



I have attempted to define the property of the OrderLine to allow it to be
nullable with the following, but the XML will still not validate



        [XmlElementAttribute(typeof(OrderLineMarking), IsNullable=true)]

        public OrderLineMarking[] OrderLineMarking



Short of creating a custom deserializer and changing the input parameter to
[XmlAnyElement]XmlElement, does anybody have a thought on how to accomplish
allowing the OrderLineMarking element to not be present and still
authenticate? If possible, I want to only change the code on the service
side.



Thanks,



Aaron

===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.