Formatting Column Values When Outputting Data as XML

Chia sẻ: Bui Tuan | Ngày: | Loại File: PDF | Số trang:3

0
38
lượt xem
7
download

Formatting Column Values When Outputting Data as XML

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

[ Team LiB ] Recipe 8.9 Formatting Column Values When Outputting Data as XML Problem You need to save some of the columns in a DataTable as attributes instead of elements when you write out the data as XML. Solution Use the ColumnMapping property.

Chủ đề:
Lưu

Nội dung Text: Formatting Column Values When Outputting Data as XML

  1. [ Team LiB ] Recipe 8.9 Formatting Column Values When Outputting Data as XML Problem You need to save some of the columns in a DataTable as attributes instead of elements when you write out the data as XML. Solution Use the ColumnMapping property. The sample code contains two event handlers: Form.Load Sets up the sample by creating a DataSet containing the first two records of the Customers table from Northwind. Refresh Button.Click Iterates over all of the columns in all of the Customers tables and sets the ColumnMapping property to the specified value. The ColumnMapping for the ContactName column is then set to the specified value. The XML output for the DataSet is displayed. The C# code is shown in Example 8-13. Example 8-13. File: XmlElementsOrAttributesForm.cs // Namespaces, variables, and constants using System; using System.Configuration; using System.Data; using System.Data.SqlClient; private DataSet ds; // . . . private void XmlElementsOrAttributesForm_Load(object sender, System.EventArgs e) {
  2. ds = new DataSet("CustomersDataSet"); // Get the top two rows from the Customers table. SqlDataAdapter da = new SqlDataAdapter( "SELECT TOP 2 * FROM Customers", ConfigurationSettings.AppSettings["Sql_ConnectString"]); da.Fill(ds, "Customers"); } private void refreshButton_Click(object sender, System.EventArgs e) { // Set the mapping type for each column in the table. foreach(DataTable table in ds.Tables) foreach(DataColumn column in table.Columns) { if(tableAttributeRadioButton.Checked) column.ColumnMapping = MappingType.Attribute; else if(tableElementRadioButton.Checked) column.ColumnMapping = MappingType.Element; else if(tableHiddenRadioButton.Checked) column.ColumnMapping = MappingType.Hidden; } // Set the mapping type for the ContactName column. DataColumn dc = ds.Tables["Customers"].Columns["ContactName"]; if(columnAttributeRadioButton.Checked) dc.ColumnMapping = MappingType.Attribute; else if(columnElementRadioButton.Checked) dc.ColumnMapping = MappingType.Element; else if(columnHiddenRadioButton.Checked) dc.ColumnMapping = MappingType.Hidden; else if(columnSimpleContentRadioButton.Checked) dc.ColumnMapping = MappingType.SimpleContent; // Display the XML. xmlTextBox.Text = ds.GetXml( ); } Discussion The ColumnMapping property of the DataColumn specifies how the value of a column will be written when the DataSet is output as an XML document with the GetXml( ) method or WriteXml( ) method. The property value is one of the MappingType
  3. enumeration values described in Table 8-5. Table 8-5. MappingType enumeration Value Description The value is written as an XML attribute. For example: Attribute my column value The value is written as an XML element. For example: Element This is the default. Hidden The value is not written in the XML output. The value is written as text in the XML element for its row. For example: SimpleContent my column value This value can only be used if the table has neither Element columns nor nested relations. There is no way to set the ColumnMapping property for all columns in a DataTable or DataSet at once. Each column must be set individually. [ Team LiB ]
Đồng bộ tài khoản