将实体框架对象实例持久保存到 xml

发布于 2024-10-21 19:33:09 字数 90 浏览 1 评论 0原文

我正在使用从具有实体框架的数据库生成的域模型。我如何将此域模型的对象实例序列化到 xml 或从 xml 反序列化?我可以使用 .edmx 文件吗?有代码示例吗? 谢谢

I am using a domain model generated from a db with entity framework. How can i serialize/deserialize an object instance of this domain model to/from xml? can i use the .edmx file for this? any code samples?
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月隐月明月朦胧 2024-10-28 19:33:09

您可以使用 XmlSerializer 类。还有 DataContractSerializer ,它是通过世界CF。例如,如果您想使用 XmlSerializer 类将现有对象序列化为 XML:

SomeModel model = ...
var serializer = new XmlSerializer(typeof(SomeModel));
using (var writer = XmlWriter.Create("foo.xml"))
{
    serializer.Serialize(writer, model);
}

并将 XML 反序列化回现有模型:

var serializer = new XmlSerializer(typeof(SomeModel));
using (var reader = XmlReader.Create("foo.xml"))
{
    var model = (SomeModel)serializer.Deserialize(reader);
}

You could use the XmlSerializer class. There is also the DataContractSerializer which was introduced with WCF. For example if you wanted to serialize an existing object to XML using the XmlSerializer class:

SomeModel model = ...
var serializer = new XmlSerializer(typeof(SomeModel));
using (var writer = XmlWriter.Create("foo.xml"))
{
    serializer.Serialize(writer, model);
}

and to deserialize back a XML to an existing model:

var serializer = new XmlSerializer(typeof(SomeModel));
using (var reader = XmlReader.Create("foo.xml"))
{
    var model = (SomeModel)serializer.Deserialize(reader);
}
百善笑为先 2024-10-28 19:33:09

我使用此 VB 代码将我的 EF 模型序列化为 Xml :

 Try
     Dim serializer = New XmlSerializer(GetType(GestionEDLService.Biens))
     Dim localFolder As StorageFolder = ApplicationData.Current.LocalFolder
     Dim sampleFile As StorageFile = Await localFolder.CreateFileAsync("dataFile.xml", CreationCollisionOption.OpenIfExists)
     Dim stream As Stream = Await sampleFile.OpenStreamForWriteAsync()

     serializer.Serialize(stream, MyEFModel.MyEntity)

 Catch ex As Exception
     Debug.WriteLine(ex.ToString)
 End Try

编辑:您还可以使用像这样的 DataContractSerializer

Imports System.Runtime.Serialization

Public Sub WriteToStream(sw As System.IO.Stream)

    Dim dataContractSerializer As New DataContractSerializer(GetType(MyDataSource))

    dataContractSerializer.WriteObject(sw, _MyDataSource)

End Sub

Public Sub ReadFromStream(sr As System.IO.Stream)

    Dim dataContractSerializer As New DataContractSerializer(GetType(MyDataSource))

    _MyDataSource = dataContractSerializer.ReadObject(sr)

End Sub

HTH

I use this VB Code to serialize my EF model to Xml :

 Try
     Dim serializer = New XmlSerializer(GetType(GestionEDLService.Biens))
     Dim localFolder As StorageFolder = ApplicationData.Current.LocalFolder
     Dim sampleFile As StorageFile = Await localFolder.CreateFileAsync("dataFile.xml", CreationCollisionOption.OpenIfExists)
     Dim stream As Stream = Await sampleFile.OpenStreamForWriteAsync()

     serializer.Serialize(stream, MyEFModel.MyEntity)

 Catch ex As Exception
     Debug.WriteLine(ex.ToString)
 End Try

EDIT: You can also use a DataContractSerializer like this

Imports System.Runtime.Serialization

Public Sub WriteToStream(sw As System.IO.Stream)

    Dim dataContractSerializer As New DataContractSerializer(GetType(MyDataSource))

    dataContractSerializer.WriteObject(sw, _MyDataSource)

End Sub

Public Sub ReadFromStream(sr As System.IO.Stream)

    Dim dataContractSerializer As New DataContractSerializer(GetType(MyDataSource))

    _MyDataSource = dataContractSerializer.ReadObject(sr)

End Sub

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文