在SOAP XML中编码char类型的元素

问题描述

| 我在编码char(unitType)时遇到一些问题。下面是来自.NET wdsl页面的示例请求。我需要知道将字符编码成哪种格式,因为直接将其放入XML是行不通的。 .NET(3.5)SOAP是否需要某些特定格式?
<?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>
传递类似
<unitType>L</unitType>
的命令无效,并且给我以下错误:   XML文档(7,37)中有错误。 --->输入字符串的格式不正确。     

解决方法

        
char
被ѭ3as序列化为数字(int)。由于您似乎正在尝试从头开始构建XML,请尝试将XML设置为
<unitType>76</unitType>
(76是
L
的值)。 我在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; }
}
输出为:
<?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>