Matthew
Matthew

Reputation: 49

How to export sql data to xml that includes an xml schema?

I am trying to export sql data as xml file with schema. The following code i create was able to export it as a xml file without a specific schema. What should i change in order to include an xml schema i want ? Thank you

 private void Button2_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlCon = new SqlConnection(connectionString))
            {
                sqlCon.Open();
                SqlDataAdapter sqlDa = new SqlDataAdapter("ViewAll", sqlCon);
                sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                sqlDa.Fill(ds, "userTB");
                ds.WriteXml(@"C:\folder\phonebookDA.xml");
            }

        }

Upvotes: 0

Views: 179

Answers (1)

Markus Deibel
Markus Deibel

Reputation: 1329

This only works with the overload WriteXml(XmlWriter, XmlWriteMode).

As per the documentation (https://learn.microsoft.com/de-de/dotnet/api/system.data.dataset.writexml?view=netframework-4.8)

[...] To write the schema, set the value for the mode parameter to WriteSchema.

Upvotes: 1

Related Questions