是否有标准XML格式将元数据添加到通用数据集?

时间:2017-02-01 19:10:18

标签: sql xml standards

我需要将从SQL查询结果生成的通用数据集保存为XML。我想在我的结果集中添加元数据以使其可逆,这样任何人都可以从XML开始创建数据库表并加载数据。 我想知道是否有一种标准的XML格式可以解决这个问题。

1 个答案:

答案 0 :(得分:0)

您没有提到您的RDBMS。对于SQL Server,您可以使用FOR XML RAW together with XMLDATA or XMLSCHEMA

DECLARE @Dummy TABLE(ID INT IDENTITY,SomeText VARCHAR(100) NOT NULL, SomeInt INT);
INSERT INTO @Dummy VALUES('row 1',1),('row 2',2);

示例XMLDATA

SELECT * FROM @Dummy FOR XML RAW,XMLDATA

<Schema name="Schema2" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <ElementType name="row" content="empty" model="closed">
    <AttributeType name="ID" dt:type="i4" />
    <AttributeType name="SomeText" dt:type="string" />
    <AttributeType name="SomeInt" dt:type="i4" />
    <attribute type="ID" />
    <attribute type="SomeText" />
    <attribute type="SomeInt" />
  </ElementType>
</Schema>
<row xmlns="x-schema:#Schema2" ID="1" SomeText="row 1" SomeInt="1" />
<row xmlns="x-schema:#Schema2" ID="2" SomeText="row 2" SomeInt="2" />

示例XMLSCHEMA(更多细节)

SELECT * FROM @Dummy FOR XML RAW,XMLSCHEMA

<xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified">
  <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
  <xsd:element name="row">
    <xsd:complexType>
      <xsd:attribute name="ID" type="sqltypes:int" use="required" />
      <xsd:attribute name="SomeText" use="required">
        <xsd:simpleType>
          <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth">
            <xsd:maxLength value="100" />
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:attribute>
      <xsd:attribute name="SomeInt" type="sqltypes:int" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>
<row xmlns="urn:schemas-microsoft-com:sql:SqlRowSet2" ID="1" SomeText="row 1" SomeInt="1" />
<row xmlns="urn:schemas-microsoft-com:sql:SqlRowSet2" ID="2" SomeText="row 2" SomeInt="2" />

Standard-Schema .Net

根据jdweng的评论,这是一种使用C#的方法:

var ds = new System.Data.DataSet("SomeName");
ds.Tables.Add("SomeTable");
ds.Tables.Add("OneMoreTable");
var t1 = ds.Tables["SomeTable"];
t1.Columns.Add("SomeInt", typeof(int));
t1.Columns.Add("SomeString", typeof(string));
var t2 = ds.Tables["OneMoreTable"];
t2.Columns.Add("SomeInt", typeof(int));
t2.Columns.Add("SomeDate", typeof(DateTime));

t1.Rows.Add(new object[] { 1, "test 1" });
t1.Rows.Add(new object[] { 2, "test 2" });
t1.Rows.Add(new object[] { 3, "test 3" });

t2.Rows.Add(new object[] { 1, DateTime.Now });

ds.WriteXml(@"c:\SomePath\test.dataset", XmlWriteMode.WriteSchema);

这是书面内容(嵌入式架构和数据)

<?xml version="1.0" standalone="yes"?>
<SomeName>
  <xs:schema id="SomeName" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="SomeName" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="SomeTable">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="SomeInt" type="xs:int" minOccurs="0" />
                <xs:element name="SomeString" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="OneMoreTable">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="SomeInt" type="xs:int" minOccurs="0" />
                <xs:element name="SomeDate" type="xs:dateTime" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <SomeTable>
    <SomeInt>1</SomeInt>
    <SomeString>test 1</SomeString>
  </SomeTable>
  <SomeTable>
    <SomeInt>2</SomeInt>
    <SomeString>test 2</SomeString>
  </SomeTable>
  <SomeTable>
    <SomeInt>3</SomeInt>
    <SomeString>test 3</SomeString>
  </SomeTable>
  <OneMoreTable>
    <SomeInt>1</SomeInt>
    <SomeDate>2017-02-02T10:23:39.2277162+01:00</SomeDate>
  </OneMoreTable>
</SomeName>