Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a C# web service accepts a custom object including unbound elements defined in XSD

I'm implementing a C# web service that is supposed to accept a custom message including unbounded number of elements.

Originally, the object is defined in a XSD file like 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 a CS file implementing the web service, I prepared a struct for this:

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

However, when I tried to access an element of the 'serverity' using a line,

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

, the program throws a 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 modified 'public string[] severity;' to 'public string severity;', it works.

Can anyone help me to make the web service to accept a message including unbounded numbers of elements?

like image 977
netgear Avatar asked Feb 21 '11 19:02

netgear


People also ask

Can I DIY AC?

While 87 percent of U.S. households use some type of air conditioning, central AC systems are the most sought-after for both convenience and accessibility. If flipping a switch for whole-house cooling is what you're after, you may wonder whether it's possible to install your own. And the answer is, you definitely can.


1 Answers

The code that corresponds to specified XSD (if serialized using XmlSerializer) is the following:

[XmlRoot("LogMessage"]
public class LogMessage
{
    [XmlElement("avantlog")]
    public LogEventType AvantLog {get; set;}
}

public class LogEventType
{
    [XmlArray("context")]
    [XmlArrayItem("severity")]
    public string[] Severity {get; set;}
}
like image 162
almaz Avatar answered Nov 15 '22 01:11

almaz