ObjectToXml(resp, ResponseZZZ);
[XmlRoot(ElementName = "response")]
public class ResponseZZZ
{
[XmlElement(ElementName = "txn_id")]
public string txn_id { get; set; }
[XmlElement(ElementName = "result")]
public string Result { get; set; }
[XmlElement(ElementName = "comment")]
public string Comment { get; set; }
}
public static XmlDocument ObjectToXml(Object obj, Class MyClass) {// Нельзя передавать Class
XmlDocument xml = new XmlDocument();
XmlSerializer formatter = new XmlSerializer(typeof(ResponseZZZ));
using (MemoryStream memStm = new MemoryStream())
{
formatter.Serialize(memStm, obj);
memStm.Position = 0;
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
using (var xtr = XmlReader.Create(memStm, settings))
{
xml = new XmlDocument();
xml.Load(xtr);
}
}
return xml;
}
class Program
{
static void Main(string[] args)
{
ObjectToXml(new object(), new ResponseZZZ());
Console.ReadLine();
}
public static XmlDocument ObjectToXml<SerializationType>(Object obj, SerializationType MyClass) where SerializationType : class, new()
{
XmlDocument xml = new XmlDocument();
XmlSerializer formatter = new XmlSerializer(typeof(SerializationType));
using (MemoryStream memStm = new MemoryStream())
{
formatter.Serialize(memStm, obj);
memStm.Position = 0;
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
using (var xtr = XmlReader.Create(memStm, settings))
{
xml = new XmlDocument();
xml.Load(xtr);
}
}
return xml;
}
}
[XmlRoot(ElementName = "response")]
public class ResponseZZZ
{
[XmlElement(ElementName = "txn_id")]
public string txn_id { get; set; }
[XmlElement(ElementName = "result")]
public string Result { get; set; }
[XmlElement(ElementName = "comment")]
public string Comment { get; set; }
}