使用 XSD 架构将 LINQ to SQL DataContext 转换为 XML

发布于 2024-10-09 23:05:28 字数 2113 浏览 1 评论 0原文

我的 Linq.DataContext 中有一些数据。

我使用以下代码成功地将其转换为 XSD - 架构:

Dim changeset As System.Data.Linq.ChangeSet = c.GetChangeSet()
Dim objDic As New Dictionary(Of System.Type, List(Of Object))

If Not changeset Is Nothing AndAlso Not changeset.Inserts Is Nothing AndAlso Not changeset.Inserts.Count = 0 Then

For Each i As Object In changeset.Inserts

    Dim t As System.Type = i.GetType()

    If objDic.ContainsKey(t) Then
        objDic(t).Add(i)
    Else
        Dim l As New List(Of Object)
        l.Add(i)
        objDic(t) = l
    End If

Next

'XML schema 

For Each t As System.Type In objDic.Keys
    Dim s As New System.IO.FileStream(String.Format("{0}{1}", LinqBulkInsert.My.Application.Info.DirectoryPath, "\xmlschema.xsd"), System.IO.FileMode.Create)

    Using xmlw As New System.Xml.XmlTextWriter(s, System.Text.Encoding.UTF8)

        xmlw.WriteStartDocument()
        xmlw.WriteStartElement("xsd:schema")
        xmlw.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
        xmlw.WriteAttributeString("xmlns:sql", "urn:schemas-microsoft-com:mapping-schema")
        xmlw.WriteStartElement("xsd:element")
        xmlw.WriteAttributeString("name", t.Name)
        xmlw.WriteAttributeString("sql:relation", c.Mapping.GetTable(t).TableName)
        xmlw.WriteStartElement("xsd:complexType")
        xmlw.WriteStartElement("xsd:sequence")

        Dim pinfos As System.Reflection.PropertyInfo() = t.GetProperties

        For Each pi As System.Reflection.PropertyInfo In pinfos
            If Not pi.PropertyType.Equals(GetType(System.Guid)) Then
                xmlw.WriteStartElement("xsd:element")
                xmlw.WriteAttributeString("name", pi.Name)
                xmlw.WriteAttributeString("type", "xsd:" & XsdTypeConverter.getInstance.GetSimpleType(pi.PropertyType))
                xmlw.WriteEndElement()
            End If
        Next
        xmlw.WriteEndDocument()
        xmlw.Flush()

    End Using

    s.Close()
    'Schema wordt succesvol weggeschreven in xmlschema.xsd

Next

现在我的问题是,如何将数据提取到完整的可读 XML 文件(其中包含所有数据)。

I have some data in my Linq.DataContext.

I had succes in converting it to an XSD - Schema, using the following code:

Dim changeset As System.Data.Linq.ChangeSet = c.GetChangeSet()
Dim objDic As New Dictionary(Of System.Type, List(Of Object))

If Not changeset Is Nothing AndAlso Not changeset.Inserts Is Nothing AndAlso Not changeset.Inserts.Count = 0 Then

For Each i As Object In changeset.Inserts

    Dim t As System.Type = i.GetType()

    If objDic.ContainsKey(t) Then
        objDic(t).Add(i)
    Else
        Dim l As New List(Of Object)
        l.Add(i)
        objDic(t) = l
    End If

Next

'XML schema 

For Each t As System.Type In objDic.Keys
    Dim s As New System.IO.FileStream(String.Format("{0}{1}", LinqBulkInsert.My.Application.Info.DirectoryPath, "\xmlschema.xsd"), System.IO.FileMode.Create)

    Using xmlw As New System.Xml.XmlTextWriter(s, System.Text.Encoding.UTF8)

        xmlw.WriteStartDocument()
        xmlw.WriteStartElement("xsd:schema")
        xmlw.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
        xmlw.WriteAttributeString("xmlns:sql", "urn:schemas-microsoft-com:mapping-schema")
        xmlw.WriteStartElement("xsd:element")
        xmlw.WriteAttributeString("name", t.Name)
        xmlw.WriteAttributeString("sql:relation", c.Mapping.GetTable(t).TableName)
        xmlw.WriteStartElement("xsd:complexType")
        xmlw.WriteStartElement("xsd:sequence")

        Dim pinfos As System.Reflection.PropertyInfo() = t.GetProperties

        For Each pi As System.Reflection.PropertyInfo In pinfos
            If Not pi.PropertyType.Equals(GetType(System.Guid)) Then
                xmlw.WriteStartElement("xsd:element")
                xmlw.WriteAttributeString("name", pi.Name)
                xmlw.WriteAttributeString("type", "xsd:" & XsdTypeConverter.getInstance.GetSimpleType(pi.PropertyType))
                xmlw.WriteEndElement()
            End If
        Next
        xmlw.WriteEndDocument()
        xmlw.Flush()

    End Using

    s.Close()
    'Schema wordt succesvol weggeschreven in xmlschema.xsd

Next

Now my question is, how can i extract the data to a complete readabke XML file (with all data in it).

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

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

发布评论

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

评论(1

画骨成沙 2024-10-16 23:05:28

尝试在您的上下文中使用 DataContract 和 DataMember 属性。阅读这些文章:

http://msdn.microsoft.com/en-us/library /bb546185.aspx
http://msdn.microsoft.com/en-us/library/bb546184.aspx

Try to use DataContract and DataMember attributes in your context. Read these articles:

http://msdn.microsoft.com/en-us/library/bb546185.aspx
http://msdn.microsoft.com/en-us/library/bb546184.aspx

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