gpt4 book ai didi

c# - 在 SOAP XML 中编码 char 类型元素

转载 作者:可可西里 更新时间:2023-11-01 16:39:26 24 4
gpt4 key购买 nike

我在编码 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> 的内容不起作用,并给我以下错误:

There is an error in XML document (7, 37). ---> Input string was not in a correct format.

最佳答案

charXmlSerializer 序列化为数字 (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>

关于c# - 在 SOAP XML 中编码 char 类型元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6301064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com