[英]How to deserialize an REST POST xml request with a one row node that hase multiple attributes?
I need some help regarding deserialization of this kind of xml in C#: 我需要一些有关在C#中对这种xml进行反序列化的帮助:
<Request>
<AccountStage att1="419749" att2="575474" att3="800177" att4="096057" att5="917185" att6="017585" att7="huKuBgcQ" att8="stgs10" att9="ACTIVE" att10="2" att11="2"/>
</Request>
If I use the "Special paste" feature from VS, and convert the request as xml classes, when I want to use the request and send it to the server, it changes the format as follows: 如果我使用VS的“特殊粘贴”功能,并将请求转换为xml类,则当我要使用请求并将其发送到服务器时,它将如下更改格式:
<Request>
<AccountStage>
<att1>22222</att1>
<att2>22222</att2>
<att3>22222</att3>
<att4>2</att4>
<att5>2</att5>
<att6>22222</att6>
<att7>Ion</att7>
<att8>agg3</att8>
<att9>ACTIVE</att9>
<att10>2</att10>
<att11>2</att11>
</AccountStage>
</Request>
Use XmlAttribute to specify how members should be defined/interpreted: 使用XmlAttribute指定应如何定义/解释成员:
using System.IO;
using System.Xml.Serialization;
namespace WpfApp2
{
internal class Test
{
private readonly string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Request>
<AccountStage att1=""419749"" att2=""575474"" att3=""800177"" att4=""096057"" att5=""917185"" att6=""017585"" att7=""huKuBgcQ""
att8=""stgs10"" att9=""ACTIVE"" att10=""2"" att11=""2"" />
</Request>";
public Test()
{
Request request;
// serialize
var serializer = new XmlSerializer(typeof(Request));
using (var reader = new StringReader(xml))
{
request = (Request) serializer.Deserialize(reader);
}
// deserialize
request.AccountStage.Attribute1 = "abcd";
using (var writer = new StringWriter())
{
serializer.Serialize(writer, request);
var s = writer.ToString();
}
}
}
public class Request
{
public AccountStage AccountStage { get; set; }
}
public class AccountStage
{
[XmlAttribute("att1")]
public string Attribute1 { get; set; }
}
}
Result 结果
<?xml version="1.0" encoding="utf-16"?>
<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AccountStage att1="abcd" />
</Request>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.