Lm th? no ? ?nh d?ng DateTime v cc gi tr? ngy trong XML chi?t xu?t t? m?t ADO.NET t?p d? li?u b?ng cch s? d?ng Visual C#.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 330597 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t t?ng b?c ny m t? cch ?nh d?ng DateTime v Ngay cc gi tr? trong XML ?c trch xu?t t? m?t ADO.NET S? li?u ?i t?ng. Trong ADO.NET, cc DateTime v Ngay gi tr? c?a DataTable c?t ?c vi?t b?ng XSD DateTime v Ngay ?nh d?ng khi cc S? li?u ?c lu d?i d?ng XML. Tiu chu?n XSD DateTime v Ngay ?nh d?ng l CCYY-MM-thng v CCYY-MM-DD, tng ?ng, b?i v? l?c ? XSD ti?m ?n c?a cc S? li?u b?n ? cc DateTime v Ngay c?t c s? d? li?u ? cc DateTime v XSD Ngay lo?i d? li?u.

T?o XML trong DateTime v Ngay cc gi tr? ?c ?i di?n trong cc ?nh d?ng tu? ch?nh c?n thi?t, s? d?ng m?t trong hai cc phng php sau y:
  • S? d?ng cc XmlConvert l?p h?c.
  • p d?ng m?t bi?n ?i XSLT (XSLT) trn cc XML ?i di?n c?a S? li?u d? li?u.

S? d?ng l?p XmlConvert

  1. B?t ?u Microsoft Visual Studio.NET. Trong Visual C#.NET, t?o ra m?t m?i ASP.?ng d?ng Web NET d? n ?c ?t tn DateTimeXmlConvert.
  2. Nh?p chu?t ph?i vo b? m?t thi?t k?WebForm1.aspx, sau b?m Xem m? ? ch?nh s?a m? trong Web m?u m? ?ng sau l?p WebForm1.aspx.cs m-un.
  3. Thm sau y vo cu?i c?a cc b?ng cch s? d?ng ch? th? ph?n t?i WebForm1.aspx.cs:
    	using System.Xml;
    	using System.Text;
    	using System.Data.SqlClient;
  4. Dn o?n m? sau trong cc Page_Load phng php:
    	
    	// Change SQL Server name, User Id and Password in following connection string.
    	string conn = "Server=SQLServerName; database=Northwind; user id=<username>; password=<strong password>";
    	SqlConnection connection = new SqlConnection();
    	connection.ConnectionString = conn;
    
    	DataSet objDataSet = new DataSet();
    	SqlDataAdapter objAdapter = new SqlDataAdapter();
    	SqlCommand objCmd = new SqlCommand();
    
    	// Retrieve the first 10 records from the employees table.
    	objCmd.CommandText = "select top 10 FirstName,BirthDate from employees";
    	objCmd.Connection = connection;
    	objAdapter.SelectCommand = objCmd;
    	objAdapter.Fill(objDataSet);
    
    	connection.Close();
    
    	// Create an instance of XmlTextReader class that reads the XML data.
    	XmlTextReader xmlReader = new XmlTextReader(objDataSet.GetXml(), XmlNodeType.Element, null);
    
    	Response.ContentType = "text/xml";
    	XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
    	xmlWriter.Indentation = 4;
    	xmlWriter.WriteStartDocument();
    	string elementName = "";
    
    	// Parse and display each node.
    	while(xmlReader.Read())
    	{
    		switch(xmlReader.NodeType)
    		{
    			case XmlNodeType.Element:
    				xmlWriter.WriteStartElement(xmlReader.Name);
    				elementName = xmlReader.Name;
    				break;
    			case XmlNodeType.Text:
    				if(elementName.ToLower() == "birthdate")
    					xmlWriter.WriteString(XmlConvert.ToDateTime(xmlReader.Value).ToString());
    				else
    					xmlWriter.WriteString(xmlReader.Value);
    				break;
    			case XmlNodeType.EndElement:
    				xmlWriter.WriteEndElement();
    				break;
    		}
    	}			
    	xmlWriter.Close();
    Chu y B?n ph?i thay ?i ID ng?i s? d?ng = <user name="">Ti kho?n gi_tr? ?n m?t trong c quy?n thch h?p ? th?c hi?n cc ho?t ?ng trn c s? d? li?u.</user>
  5. Lu cc thay ?i ? WebForm1.aspx.cs.
  6. Trn cc Xy d?ng tr?nh n, nh?p vo Xy d?ng Gi?i php.
  7. Ch?y Microsoft Internet Explorer v m? WebForm1.aspx b?i cch xc ?nh URL sau, ni IISServerName la tn my ch? Microsoft Internet Information Services (IIS) c?a b?n:
    http://IISServerName/DateTimeXmlConvert/WebForm1.aspx
By gi? b?n c th? th?y cc t?p tin XML k?t xu?t trong tr?nh duy?t trong cc DateTime d?ng ty ch?nh.

p d?ng m?t bi?n ?i XSLT trn ?i di?n XML t?p d? li?u d? li?u

B?n c th? s? d?ng inline k?ch b?n kh?i v cc thnh ph?n bn ngoi m?, c?n ?c g?i l XSLT m? r?ng ?i t?ng, ? th?c hi?n cng vi?c tu? ch?nh. o la vi?n d?n trong di?n XSLT th?c hi?n tnh ton v x? l? d? li?u. Microsoft khuy?n co r?ng b?n trnh s? d?ng cc t?p l?nh n?i tuy?n kh?i. B?n c th? s? d?ng ph?n m? r?ng cc ?i t?ng ? th?c hi?n cng vi?c tu? ch?nh khi b?n thi?t k? xch tay XSLT phong cch t?.

T?o m?t ASP.NET Web m?u

  1. T?o ra m?t m?i Visual C#.NET ASP.D? n ?ng d?ng Web NET ?t tn DateTimeXSLT.
  2. Thm m?t m-un h?ng m?i tn l DateConverter.cs ? cc d? n. M? i?u ny trong tr?nh so?n th?o m?, v sau xa cc m? hi?n c.
  3. Dn o?n m? sau trong l?p DateConverter.cs m-un ? th?c hi?n cc DateConverter l?p:
    using System;
    
    namespace DateTimeXSLT
    {
    	public class DateConverter
    	{
    		public string GetDateTime(string data, string format)
    		{
    			DateTime dt = DateTime.Parse(data);
    			return dt.ToString(format);
    		}
    	}
    }
    
  4. Lu cc thay ?i ? DateConverter.cs.
  5. Nh?p chu?t ph?i vo b? m?t thi?t k?WebForm1.aspx, sau b?m Xem m? ? ch?nh s?a m? trong m?u Web m? ?ng sau l?p WebForm1.aspx.cs m-un.
  6. Thm sau y vo cu?i c?a cc b?ng cch s? d?ng ch? th? ph?n t?i WebForm1.aspx.cs:
    	using System.IO;
    	using System.Xml;
    	using System.Xml.Xsl;
    	using System.Xml.XPath;
    	using System.Data.SqlClient;
  7. Dn o?n m? sau trong cc Page_Load phng php:
    	// Change Sql Server name, User Id and Password in following connection string.
    	string strConn = "Server=SQLServerName;database=Northwind; user id=<username>; password=<strong password>;
    	SqlConnection connection = new SqlConnection();
    	connection.ConnectionString = strConn;
    
    	DataSet objDataSet = new DataSet();
    	SqlDataAdapter objAdapter = new SqlDataAdapter();
    	SqlCommand objCmd = new SqlCommand();
    	// Retrieve all records from employees table.
    	objCmd.CommandText = "select FirstName,BirthDate from employees";
    	objCmd.Connection = connection;
    	objAdapter.SelectCommand = objCmd;
    	objAdapter.Fill(objDataSet);
    
    	connection.Close();
    
    	// Create an instance of StringReader class that reads the XML data.
    	StringReader reader = new StringReader(objDataSet.GetXml());
    	XPathDocument doc = new XPathDocument(reader);
    
    	// Create an XslTransform object and load xslt file.
    	XslTransform transform = new XslTransform();
    	transform.Load(this.MapPath("DateTime.xslt"));
    
    	//Add an object to convert  DateTime format.
    	DateConverter objDateConverter =		new DateConverter();
    	XsltArgumentList args = new XsltArgumentList();
    	args.AddExtensionObject("urn:ms-kb", objDateConverter);
    
    	transform.Transform(doc, args, Response.OutputStream);
    	
  8. Lu cc thay ?i ? WebForm1.aspx.cs.

T?o m?t ti li?u XSLT m?u

  1. Thm t?p XSLT m?i tn l DateTime.xslt ? DateTimeXSLT ASP.NET Web d? n.
  2. Thay th? m? ?c t?o ra v?i o?n m? sau. S? d?ng Notepad nh m?t cng c? trung gian, n?u b?n c b?t k? m? ha ho?c k? t? v?n ? khi b?n c? g?ng dn m? ny trong t?p tin:
    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:myObj="urn:ms-kb">
    
    <xsl:template match="NewDataSet">
    <table><xsl:attribute name="border">1</xsl:attribute>
       <TR><TD>Employee name</TD><TD>Original DateTime Format</TD><TD>Changed DateTime Format</TD></TR>
    	<xsl:apply-templates select="*"/>
    </table>
    </xsl:template>
    
    <xsl:template match="*">
    	<TR>
    	<xsl:apply-templates select="*"/>
    	</TR>
    </xsl:template>
    
    <xsl:template match="FirstName">
    <TD>
    	<xsl:value-of select="."/>
    </TD>
    </xsl:template>
    
    <xsl:template match="BirthDate">
    <xsl:variable name="Date" select="."/>
    <TD><xsl:value-of select="$Date"/></TD>
    <TD>	
    	<xsl:value-of select="myObj:GetDateTime($Date, 'F')"/>
    </TD>
    </xsl:template>
    
    </xsl:stylesheet>
    
  3. Lu cc thay ?i ? DateTime.xslt.

Ki?m tra s? chuy?n ?i XSLT b?ng cch s? d?ng ASP.NET Web m?u

  1. Lu cc thay ?i ? DateTimeXSLT ASP.NET Web d? n.
  2. Trn cc Xy d?ng tr?nh n, nh?p vo Xy d?ng Gi?i php.
  3. Ch?y Microsoft Internet Explorer v m? WebForm1.aspx b?i cch xc ?nh URL sau, ni IISServerName la tn my ch? IIS c?a b?n:
    http://IISServerName/DateTimeXSLT/WebForm1.aspx
By gi? b?n c th? th?y cc DateTime ?nh d?ng c?a cc S? li?u v vi?c chuy?n DateTime ?nh d?ng trn trang WebForm1.aspx.

THAM KH?O

? bi?t thm thng tin, h?y b?m vao s? bi vi?t sau ? xem bi vi?t trong C s? Ki?n th?c Microsoft:
323370Lm th? no ? s? d?ng ph?n m? r?ng ?i t?ng khi b?n th?c hi?n XSL transformations trong Visual C#.Cc ?ng d?ng m?ng

Thu?c tnh

ID c?a bi: 330597 - L?n xem xt sau cng: 28 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft ADO.NET 1.1
  • Microsoft ASP.NET 1.0
  • Microsoft ASP.NET 1.1
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
T? kha:
kbwebforms kbdatetime kbhowtomaster kbmt KB330597 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:330597

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com