Using XmlMetadataSource
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
The XmlMetadataSource class allows you to create (load) a MetadataContainer from a XML based source. This tutorial demonstrates you how to load a MetadataContainer from an existing RLINQ file by using the XmlMetadataSource the class.
The XmlMetadataSource class, exposes five static factory methods allowing you to create a new XmlMetadataSource instance from different objects. When you have an instance of the XmlMetadataSource class, you have to invoke the GetModel method to access the loaded metadata container.
-
FromStream - the FromStream static method allows you to create a new XmlMetadataSource instance from a given stream object.
string path = @"C:\EntityDiagrams.rlinq"; Telerik.OpenAccess.Metadata.XmlMetadataSource xmlMetadataSource = Telerik.OpenAccess.Metadata.XmlMetadataSource .FromStream(File.Open(path, FileMode.Open)); Telerik.OpenAccess.Metadata.MetadataContainer model = xmlMetadataSource.GetModel();
Dim path As String = "C:\EntityDiagrams.rlinq" Dim xmlMetadataSource As Telerik.OpenAccess.Metadata.XmlMetadataSource = _ Telerik.OpenAccess.Metadata.XmlMetadataSource _ .FromStream(File.Open(path, FileMode.Open)) Dim model As Telerik.OpenAccess.Metadata.MetadataContainer = xmlMetadataSource.GetModel()
-
FromReader - the FromReader static method allows you to create a new XmlMetadataSource instance from a XmlReader object.
string path = @"C:\EntityDiagrams.rlinq"; XmlReader xmlReader = XmlReader.Create(path); Telerik.OpenAccess.Metadata.XmlMetadataSource xmlMetadataSource = Telerik.OpenAccess.Metadata.XmlMetadataSource.FromReader(xmlReader); Telerik.OpenAccess.Metadata.MetadataContainer model = xmlMetadataSource.GetModel();
Dim path As String = "C:\EntityDiagrams.rlinq" Dim xmlReader_Renamed As XmlReader = XmlReader.Create(path) Dim xmlMetadataSource As Telerik.OpenAccess.Metadata.XmlMetadataSource = _ Telerik.OpenAccess.Metadata.XmlMetadataSource.FromReader(xmlReader_Renamed) Dim model As Telerik.OpenAccess.Metadata.MetadataContainer = xmlMetadataSource.GetModel()
FromUrl - creates a XmlMetadataSource instance with a given URL.
-
FromXml - creates a new XmlMetadataSource instance from a given XML as a string. The following example demonstrates you how to use the FromXml method. First, an instance of the assembly containing the RLINQ file is created. Next, all assembly resources are iterated and the target RLINQ file is extracted. A XmlDocument object is initialized with the RLINQ file. Finally, the outer XML is taken and passed to the FromXml method. Note that in this example the BuildAction of the RLINQ file should be set to Embedded Resource. EntityDiagrams is the name of the RLINQ file.
XmlDocument doc = new XmlDocument(); Assembly assembly = Assembly.GetExecutingAssembly(); Stream stream = null; string[] resources = assembly.GetManifestResourceNames(); foreach (var item in resources) { if (item.EndsWith("EntityDiagrams.rlinq")) { stream = assembly.GetManifestResourceStream(item); break; } } if (stream != null) { doc.Load(stream); stream.Dispose(); } if (doc != null) { Telerik.OpenAccess.Metadata.XmlMetadataSource xmlMetadataSource = Telerik.OpenAccess.Metadata.XmlMetadataSource.FromXml(doc.OuterXml); Telerik.OpenAccess.Metadata.MetadataContainer model = xmlMetadataSource.GetModel(); }
Dim doc As New XmlDocument() Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly() Dim stream_Renamed As Stream = Nothing Dim resources() As String = assembly.GetManifestResourceNames() For Each item In resources If item.EndsWith("EntityDiagrams.rlinq") Then stream_Renamed = assembly.GetManifestResourceStream(item) Exit For End If Next item If stream_Renamed IsNot Nothing Then doc.Load(stream_Renamed) stream_Renamed.Dispose() End If If doc IsNot Nothing Then Dim xmlMetadataSource As Telerik.OpenAccess.Metadata.XmlMetadataSource = _ Telerik.OpenAccess.Metadata.XmlMetadataSource.FromXml(doc.OuterXml) Dim model As Telerik.OpenAccess.Metadata.MetadataContainer = xmlMetadataSource.GetModel() End If
-
FromAssemblyResource - creates a new XmlMetadataSource instance from an assembly resource. Suppose you have the following project structure:
And the BuildAction of the EntityDiagrams.rlinq is set to Embedded Resource. Then the FromAssemblyResource static method can be used in the following manner.
Telerik.OpenAccess.Metadata.XmlMetadataSource xmlMetadataSource = Telerik.OpenAccess.Metadata.XmlMetadataSource.FromAssemblyResource(Assembly .GetExecutingAssembly(), "EntityDiagrams.rlinq"); Telerik.OpenAccess.Metadata.MetadataContainer model = xmlMetadataSource.GetModel();
Dim xmlMetadataSource As Telerik.OpenAccess.Metadata.XmlMetadataSource = _ Telerik.OpenAccess.Metadata.XmlMetadataSource.FromAssemblyResource(System.Reflection _ .Assembly.GetExecutingAssembly(), "EntityDiagrams.rlinq") Dim model As Telerik.OpenAccess.Metadata.MetadataContainer = xmlMetadataSource.GetModel()