Formatting Column Values When Outputting Data as XML
Số trang: 3
Loại file: pdf
Dung lượng: 14.98 KB
Lượt xem: 1
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ 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.
Nội dung trích xuất từ tài liệu:
Formatting Column Values When Outputting Data as XML[ Team LiB ]Recipe 8.9 Formatting Column Values When Outputting Data as XMLProblemYou need to save some of the columns in a DataTable as attributes instead of elementswhen you write out the data as XML.SolutionUse 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 constantsusing System;using System.Configuration;using System.Data;using System.Data.SqlClient;private DataSet ds;// . . .private void XmlElementsOrAttributesForm_Load(object sender, System.EventArgs e){ 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( );}DiscussionThe ColumnMapping property of the DataColumn specifies how the value of a columnwill 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 MappingTypeenumeration 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 orDataSet at once. Each column must be set individually.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Formatting Column Values When Outputting Data as XML[ Team LiB ]Recipe 8.9 Formatting Column Values When Outputting Data as XMLProblemYou need to save some of the columns in a DataTable as attributes instead of elementswhen you write out the data as XML.SolutionUse 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 constantsusing System;using System.Configuration;using System.Data;using System.Data.SqlClient;private DataSet ds;// . . .private void XmlElementsOrAttributesForm_Load(object sender, System.EventArgs e){ 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( );}DiscussionThe ColumnMapping property of the DataColumn specifies how the value of a columnwill 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 MappingTypeenumeration 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 orDataSet at once. Each column must be set individually.[ Team LiB ]
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Formatting Column Values When Outputting Data as XMLGợi ý tài liệu liên quan:
-
52 trang 430 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 314 0 0 -
74 trang 299 0 0
-
96 trang 293 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 275 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 265 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 265 0 0