Encoding char type element in SOAP XML
I am having some problems encoding the char (unitType). Below is the example request from the .NET wdsl page. I need to know what format to encode the character into, because putting it straight into the XML does not work. Does .NET (3.5) SOAP require some specific format?
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<DiagnosticAnalysis xmlns="http://www.somewhere.com/">
<tuple>int</tuple>
<unitID>int</unitID>
<unitType>char</unitType>
</DiagnosticAnalysis>
</soap12:Body>
</soap12:Envelope>
开发者_Python百科Passing in something like <unitType>L</unitType>
does not work, and gives me the error below:
There is an error in XML document (7, 37). ---> Input string was not in a correct format.
char
is serialized as a number (int) by the XmlSerializer
. Since it appears you are trying to build the XML from scratch, try setting the XML to <unitType>76</unitType>
(76 is the value for L
).
I tested using the following code in LinqPad:
void Main()
{
var m = new MyClass();
m.UnitType = 'L';
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyClass));
using(var sr = new StringWriter())
{
serializer.Serialize(sr, m);
Console.WriteLine(sr.GetStringBuilder().ToString());
}
}
public class MyClass
{
public char UnitType { get; set; }
}
Output is:
<?xml version="1.0" encoding="utf-16"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UnitType>76</UnitType>
</MyClass>
精彩评论