问题
I want to save a list of dates in an xml, I want to use dataset to achieve the task, I do the same to a database using Entity Framework. This allows me to access the dates using event.eventDates.start
but in the dataset I cannot achieve it.
public class Event
{
[Key]
public string id { get; set; }
public virtual ICollection<Date> eventDates { get; set; }
}
Date class
public class Date
{
public DateTime start { get; set; }
public DateTime end { get; set; }
}
When using entity framework I can access the eventDates Object using event.eventDates.start
I mapped the data from the sql database in the dataset builder the relations look like this
I want the xml file to be in this format
<?xml version="1.0" standalone="yes"?>
<db xmlns="http://tempuri.org/LocalDB.xsd">
<Event>
<id>ID</id>
<eventdates>
<date>
<startdate></startdate>
<enddate></enddate>
<date>
<date>
<startdate></startdate>
<enddate></enddate>
<date>
</eventdates>
</Event>
</db>
Is there any way to achieve that using datasets? I'm new to C# any help would be appreciated
回答1:
This code works, based on generate xml files based on my c# classes. I would rename your classes to something else than "Event" and "Date". These names are too generic and are usually protected by the system. ICollection is not serializable. Read XML serialization of interface property. You can use DTO or you can change the collection type (i.e. with List<>) and with XML serialization attributes avoid circular references and/or disable lazy load (i.e. use eagerly load using Include method) or the risk is that you serialize the whole database.
Program.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace EFToXml
{
public class MyEvent
{
public string Id { get; set; }
public virtual List<MyDate> EventDates { get; set; }
}
public class MyDate
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
var myDate1 = new MyDate
{
Start = DateTime.Now,
End = DateTime.Now.AddDays(1)
};
var eventDates = new List<MyDate> { myDate1 };
var myEvent = new MyEvent
{
Id = "1",
EventDates = eventDates
};
XmlSerializer serializer = new XmlSerializer(typeof(MyEvent));
serializer.Serialize(File.Create(@"C:\Users\<UserName>\Source\Repos\myEvents.xml"), myEvent);
}
}
}
myEvents.xml:
<?xml version="1.0"?>
<MyEvent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Id>1</Id>
<EventDates>
<MyDate>
<Start>2019-11-24T21:52:04.5032671+01:00</Start>
<End>2019-11-25T21:52:04.5194026+01:00</End>
</MyDate>
</EventDates>
</MyEvent>
回答2:
If i understand correctly, you just want to reproduce EntityFramework's behavior using DataSets. You can achieve exactly same thing simply creating DataSet (using integrated VisualStudio designer) with proper tables, fields and relations:
later on you can access your data using code like this:
var ds = new DataSet1();
var ue = ds.UserEvents.FirstOrDefault();
var ued = ue.GetChildRows("FK_UserEvents_EventDates")
.Cast<DataSet1.EventDatesRow>();
var date = ued.FirstOrDefault().Date;
Next thing to do is serialization - it's quite easy: Serialization example
来源:https://stackoverflow.com/questions/59020623/is-there-a-way-to-save-a-list-of-objects-in-dataset-in-c-sharp