Last active
November 25, 2019 12:07
-
-
Save mgravell/503a885c291a4eeee2be201473059f75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Xml.Serialization; | |
public class EnquiryResponseInfo | |
{ | |
public string EnquiryId { get; set; } | |
public EnquiryViewModel Enquiry { get; set; } | |
} | |
public class EnquiryViewModel | |
{ | |
public string Name { get; set; } | |
public string Address { get; set; } | |
} | |
static class P | |
{ | |
static void Main() | |
{ | |
var list = new List<EnquiryResponseInfo> | |
{ | |
new EnquiryResponseInfo | |
{ | |
EnquiryId = "xxx", | |
Enquiry = null, | |
}, | |
new EnquiryResponseInfo | |
{ | |
EnquiryId = "yyy", | |
Enquiry = new EnquiryViewModel | |
{ | |
Name = "Test user", | |
Address = "Test Address" | |
} | |
}, | |
}; | |
var xml = Serialize(list); | |
Console.WriteLine(xml); | |
/* | |
<ArrayOfEnquiryResponseInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<EnquiryResponseInfo> | |
<EnquiryId>xxx</EnquiryId> | |
</EnquiryResponseInfo> | |
<EnquiryResponseInfo> | |
<EnquiryId>yyy</EnquiryId> | |
<Enquiry> | |
<Name>Test user</Name> | |
<Address>Test Address</Address> | |
</Enquiry> | |
</EnquiryResponseInfo> | |
</ArrayOfEnquiryResponseInfo> | |
*/ | |
} | |
public static string Serialize<T>(T toSerialize) | |
{ | |
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); | |
using (StringWriter textWriter = new StringWriter()) | |
{ | |
xmlSerializer.Serialize(textWriter, toSerialize); | |
return textWriter.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment