Jason Lee
Jason Lee

Reputation: 23

Create XSD and XML document from dataset where xml references xsd Import xml to excel

Long title but hopefully I can explain what I am after. I have read and googled a ton of xml documentation but haven't quite gotten an answer to what Im trying to do. I am trying to create an XSD document that generates explicit type definitions for fields in the dataset. I am then trying to create a sample XML document that validates against the XSD document.

This works fine inside of visual studio because I can read in the schema and the document and it validates BUT, the xml document created has no real reference to the XSD via xlmns or other method. If I want to use this xml data in say MS Excel and I try to import it doesnt know where the XSD is to validate against so it assumes it based on the values. Is there a way in my code to add the xlmns to both the XSD and XML if that is what is needed to solve this?

 Dim MyDataSet As New DataSet("Samples")

    Dim MyDataTable As New DataTable("Sample")

    MyDataSet.Tables.Add(MyDataTable)

    MyDataTable.Columns.Add(New DataColumn("blnExample", GetType(Boolean), Nothing, MappingType.Element))
    MyDataTable.Columns.Add(New DataColumn("datExample", GetType(DateTime), Nothing, MappingType.Element))
    MyDataTable.Columns.Add(New DataColumn("decExample", GetType(Decimal), Nothing, MappingType.Element))
    MyDataTable.Columns.Add(New DataColumn("dblExample", GetType(Double), Nothing, MappingType.Element))
    MyDataTable.Columns.Add(New DataColumn("intExample", GetType(Integer), Nothing, MappingType.Element))
    MyDataTable.Columns.Add(New DataColumn("lngExample", GetType(Long), Nothing, MappingType.Element))
    MyDataTable.Columns.Add(New DataColumn("sglExample", GetType(Single), Nothing, MappingType.Element))
    MyDataTable.Columns.Add(New DataColumn("strExample", GetType(String), Nothing, MappingType.Element))

    ' Write out the XSD

    MyDataSet.WriteXmlSchema(Server.MapPath("~\xml\MySchema.xsd"))

    ' Put some data in the table
    Dim TempRow As DataRow

    For i As Integer = 1 To 3

        TempRow = MyDataTable.NewRow()
        TempRow("blnExample") = True
        TempRow("datExample") = New DateTime(2012, 2, 29)
        TempRow("decExample") = 1
        TempRow("dblExample") = 2
        TempRow("intExample") = 3
        TempRow("lngExample") = 4
        TempRow("strExample") = "Example"
        MyDataTable.Rows.Add(TempRow)
    Next
    ' Write out the data        

    MyDataSet.WriteXml(Server.MapPath("~\xml\MyData.xml"))

Upvotes: 0

Views: 1611

Answers (1)

DRH
DRH

Reputation: 8366

You can add a namespace to both documents by setting the Namespace property on your DataSetinstance:

MyDataSet.Namespace = "http://www.example.com/"

This will result in your schema having a target namespace:

<xs:schema targetNamespace="http://www.example.com/" ... >

and your instance document referencing that namespace:

<Sample xmlns="http://www.example.com/">

Upvotes: 0

Related Questions