public async Task<FileContentResult> Export()
{
// Выключаем Proxy при запросах, иначе сериализация выпадает с ошибкой.
using (DefaultConnection conn = new DefaultConnection())
{
conn.Configuration.ProxyCreationEnabled = false;
// получаем нашу структуру с вложенными свойствами
List<OurUnit> Units = conn.Units.Where(u => u.UserId == userId).Include(a => a.SubUnits).ToList();
XmlSerializer serializer = new XmlSerializer(typeof(List<OurUnit>));
StringWriter writer = new StringWriter();
serializer.Serialize(writer, Units);
System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding();
byte[] writerToBytes = encoding.GetBytes(writer.ToString());
FileContentResult file = new FileContentResult(writerToBytes, "text/xml");
file.FileDownloadName = CurrentUser.UserName + " data " + DateTime.Now.ToString() + ".xml";
conn.Configuration.ProxyCreationEnabled = true;
return file;
}
class Program
{
static void Main(string[] args)
{
List<XElement> elements = GetElementsFromFile(@"ConsoleApplication35.exe.config");
foreach (var item in elements)
{
Console.WriteLine(item.Name);
}
Console.ReadKey();
}
public static List<XElement> GetElementsFromFile(string p_fileName)
{
List<XElement> list = new List<XElement>();
BuildElementList(XElement.Load(p_fileName).Elements(), list);
return list;
}
private static void BuildElementList(IEnumerable<XElement> p_elements, List<XElement> p_list)
{
foreach (var item in p_elements)
{
p_list.Add(item);
var children = item.Elements();
if (children.Any())
{
BuildElementList(children, p_list);
}
}
}
}