我有以下 C# 类
[XmlRoot("Customer")]
public class MyClass
{
[XmlElement("CustId")]
public int Id {get;set;}
[XmlElement("CustName")]
public string Name {get;set;}
}
然后我使用以下函数将类对象序列化为 XML
public static XmlDocument SerializeObjectToXML(object obj, string sElementName)
{
XmlSerializer serializer =
new XmlSerializer(obj.GetType(), new XmlRootAttribute("Response"));
using (MemoryStream ms = new MemoryStream())
{
XmlDocument xmlDoc = new XmlDocument();
serializer.Serialize(ms, obj);
ms.Position = 0;
xmlDoc.Load(ms);
}
}
我当前对 XML 的输出是这样的;
<Response>
<CustId></CustId>
<CustName></CustName>
</Response>
但是我怎样才能得到这样的响应;
<Response>
<Customer>
<CustId></CustId>
<CustName></CustName>
</Customer>
</Response>