序列化类为xml使用RestSharp.AddBody失败 [英] Serializing a class to xml using RestSharp.AddBody fails
问题描述
我尝试了一个简单的测试类添加到 RestSharp-RestRequest
通过 RestRequest.AddBody
- 方法。我试图同时使用提供序列化序列化,但我不能让他们中的一个工作(JSON-序列化的作品只用相同的设置非常精细...)
这是我做的序列化:
私人无效SerializationTest()
{
RestRequest要求=新RestRequest();
request.XmlSerializer =新RestSharp.Serializers.DotNetXmlSerializer();
//request.XmlSerializer =新RestSharp.Serializers.XmlSerializer();
request.RequestFormat = DataFormat.Xml;
//request.RequestFormat = DataFormat.Json;
request.AddBody(新假人()); //使用JsonSerializer
label1.Text = request.Parameters [0] .Value.ToString();
}
我使用的是哑类是:
私有类假
{
公共字符串A =一些字符串;
公共字符串B =一些字符串;
}
-
使用
RestSharp.Serializers.XmlSerializer()
我得到:<虚拟/>
(缺少两个字符串) -
使用
RestSharp.Serializers.DotNetXmlSerializer()
我什么也没得到,在刚刚PROGRAMM得到忽略了最低在序列化的一步。 -
使用JSON
request.RequestFormat = DataFormat.Json;
,一切工作正常。
{
A:一些字符串,
b:一些字符串
}
我如何获得类,所以适当的序列化到XML?结果
感谢您的帮助!
这些都是领域,而不是属性。底层的XmlSerializer只查找公共属性。它更新到这一点,它应该工作:
类假
{
公共字符串A {搞定;组; };
公共字符串B {搞定;组; };
公共假人(){
A =一些字符串;
B =一些字符串;
}
}
JSON的作品之一,是因为它推迟到JSON.NET的默认串行这(显然)支持领域。我认为这是错误的设计决策往心里去。
I try to add a simple test-class to a RestSharp-RestRequest
via the RestRequest.AddBody
-Method. I tried to serialize using both of the delivered serializers, but i could not get one of them to work (JSON-Serializations works pretty fine with just the same settings...)
This is how i do the serialization:
private void SerializationTest()
{
RestRequest request = new RestRequest();
request.XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer();
//request.XmlSerializer = new RestSharp.Serializers.XmlSerializer();
request.RequestFormat = DataFormat.Xml;
//request.RequestFormat = DataFormat.Json;
request.AddBody(new Dummy()); // uses JsonSerializer
label1.Text = request.Parameters[0].Value.ToString();
}
The dummy-class I'm using is:
private class Dummy
{
public string A = "Some string";
public string B = "Some string";
}
Using
RestSharp.Serializers.XmlSerializer()
I get: "<Dummy />
" (missing both strings)Using
RestSharp.Serializers.DotNetXmlSerializer()
I get nothing, the programm just dosen't get over the serialization-step.Using JSON
request.RequestFormat = DataFormat.Json;
, everything works fine.
.
{
"A": "Some string",
"B": "Some string"
}
How do i get the class so serialize proper to XML?
Thanks for your help!
Those are fields, not properties. The underlying XmlSerializer only looks for public properties. Update it to this and it should work:
class Dummy
{
public string A { get; set; };
public string B { get; set; };
public Dummy() {
A = "Some string";
B = "Some string";
}
}
The reason the JSON one works is because it defers to JSON.NET's default serializer which (apparently) supports fields. I think this is the wrong design decision personally.
这篇关于序列化类为xml使用RestSharp.AddBody失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!