How to force a C # web service to accept a custom object, including unrelated elements defined in XSD

I am implementing a C # web service that should receive a custom message, including an unlimited number of elements.

Initially, an object is defined in an XSD file, as shown below:

<xsd:element name="LogMessage"> <xsd:complexType> <xsd:sequence> <xsd:element minOccurs="1" maxOccurs="1" name="avantlog" type="tns:LogEventType"> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="LogEventType"> <xsd:sequence> <xsd:element minOccurs="1" maxOccurs="1" name="context" type="tns:ContextType"> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:complexType name="ContextType"> <xsd:sequence> <xsd:element minOccurs="1" maxOccurs="unbounded" name="severity" type="xsd:string"> </xsd:element> </xsd:sequence> </xsd:complexType> 

And in the CS file that implements the web service, I prepared the structure for this:

 public struct logevent { public ContextType context; public struct ContextType { public string[] severity; } } 

However, when I tried to access the "serverity" element using a string,

 String temp = logevent.context.severity.GetValue(0).ToString() 

The program produces the following error:

 "Index was outside the bounds of the array." 

When I changed the element from 'unbounded' to '1' in the XSD file, and also changed the 'public string [] severity; for "public line seriousness," it works.

Can someone help me get a web service to accept a message, including an unlimited number of elements?

+6
c # web-services
source share
3 answers

The code corresponding to the specified XSD (if serialized using the XmlSerializer) is as follows:

 [XmlRoot("LogMessage"] public class LogMessage { [XmlElement("avantlog")] public LogEventType AvantLog {get; set;} } public class LogEventType { [XmlArray("context")] [XmlArrayItem("severity")] public string[] Severity {get; set;} } 
+1
source share

You may need to use attributes to control the deserialization of incoming XML. By default, the supported XML structure for arrays follows the form:

 <Elements> <Element>X</Element> <Element>Y</Element> </Element> 

However, your WSDL indicates the unlimited term "Element" and does not provide for the parent block "Elements". I understand that to use unlimited terms you need to specify attributes to control deserialization, since unlimited terms are not standard for the generation and deserialization of .NET WSDL.

This article discusses how to control deserialization using attributes:

http://msdn.microsoft.com/en-us/library/2baksw0z.aspx

0
source share

You can convert XSD to a POCO object using "XSD.exe" and then use XmlSerializer. This will simplify interaction with multiple external systems via XML. You can also use SGen.exe to increase the performance of XmlSerializer . Hope this helps

http://msdn.microsoft.com/en-us/library/x6c1kb0s (v = vs .71) .aspx

http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm

0
source share

All Articles