使用Vb中的数据集和XSD文件生成Xml文件

时间:2015-07-29 08:17:26

标签: xml vb.net xsd dataset

如何使用数据集生成Xml文件,数据集由数据库填充,遵循XSD文件中的规则?

编辑:

这就是我写的:

        ' Create a DataSet with one table 
        ' containing two columns and 10 rows.
        Dim dataSet As DataSet = New DataSet("dataSet")
        Dim table As DataTable = dataSet.Tables.Add("Items")
        table.Columns.Add("id", Type.GetType("System.Int32"))
        table.Columns.Add("Item", Type.GetType("System.String"))

        ' Add ten rows
        Dim row As DataRow
        Dim i As Integer
        For i = 0 To 9
            row = table.NewRow()
            row("id") = i
            row("Item") = "Item" & i
            table.Rows.Add(row)
        Next


        Dim strm As StreamReader = New StreamReader("schema.xsd")
        Dim filename As String = "output.xml"
        Dim stream As New System.IO.FileStream(filename, System.IO.FileMode.Create)
        dataSet.ReadXmlSchema(strm)
        dataSet.WriteXml(stream)

我更改了XSD文件,重命名了这样的元素:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="dataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="dataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Items">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="id-test" type="xs:int" minOccurs="0" />
              <xs:element name="Item-test" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

但是当我生成XML文件时,元素的名称仍然是id和item。

0 个答案:

没有答案