我有一个xml字符串,其结构如下:
<soapenv:Envelope xmlns:soapenv="http://someurl">
xmlns:v5="http://someotherurl"
xmlns:v51="http://evensomeotherurl">
<soapenv:Header>
<v5:Client> APP </v5:Client>
</soapenv:Header>
<soapenv:Body>
<v51:SomeAction>
<v51:Value>1<v/51:Value>
</v51:SomeAction>
</soapenv:Body>
</soapenv:Envelope>
如果我使用这个网站
https://xmltocsharp.azurewebsites.net/
,我得到这个类转换:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="Header", Namespace="http://someurl")]
public class Header {
[XmlElement(ElementName="Client", Namespace="http://someotherurl")]
public string Client { get; set; }
}
[XmlRoot(ElementName="SomeAction", Namespace="http://evensomeotherurl")]
public class SomeAction {
[XmlElement(ElementName="Value", Namespace="http://evensomeotherurl")]
public string Value { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://someurl")]
public class Body {
[XmlElement(ElementName="SomeAction", Namespace="http://evensomeotherurl")]
public SomeAction SomeAction { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://someurl")]
public class Envelope
{
[XmlElement(ElementName="Header", Namespace="http://someurl")]
public Header Header { get; set; }
[XmlElement(ElementName="Body", Namespace="http://someurl")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="soapenv", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenv { get; set; }
[XmlAttribute(AttributeName="v5", Namespace="http://www.w3.org/2000/xmlns/")]
public string V5 { get; set; }
[XmlAttribute(AttributeName="v51", Namespace="http://www.w3.org/2000/xmlns/")]
public string V51 { get; set; }
}
}
问题是,这个类不再序列化为原始字符串。我如何纠正装饰器,以便在序列化此对象的实例后,我得到的字符串与反序列化前完全相同。
编辑
这是我序列化的方式:
public string ToXml<T>(T obj)
{
using(var stringwriter = new System.IO.StringWriter())
{
var serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(stringwriter, obj);
return HttpUtility.HTMLDecode(stringwriter.ToString())
}
}