SQL er klar lettest at vedliegholde osv.
Men XML data er for nogle skønnere at bearbejde eller udveksle.
Men hvis du i sql kaldet siger where 1=1 for xml auto; returnere sqlserveren xml, dvs du skal ikke til at fedte med at danne det:
ex på en hurtigt httphandler jeg bruegr til at skubbe lidt data ud som xml
- <%@ WebHandler Language="C#" Class="data" %>
-
- using System;
- using System.Web;
- using System.Data.SqlClient;
- using System.Xml;
- using System.Text;
- using System.IO.Compression;
- using System.IO;
-
- public class data : IHttpHandler {
-
- public void ProcessRequest (HttpContext context) {
- context.Response.ContentType = "application/xml";
-
-
- using(SqlConnection s = new SqlConnection(@"server=SERVER1\WINKOMPASEXPRESS;database=wincom;"))
- {
- s.Open();
-
- string CommandString = @"select
- c.customer as cId,
- c.[name] as firmanavn,
- c.contact as cPerson1,
- c.address1 as [add],
- c.Address2 as add2,
- c.Email as main,
- c.PostalCode as zip,
- c.Phone as phone,
- c.Fax as fax,
- c.Remarks as [desc],
- c.InterNet as web,
- c.City as city,
- cast(c.OptionalNumber2 as int) as emps,
- c.optional1 as o1,
- c.optional2 as o2,
- cc.[Customer] as ccId
- ,cc.[Name] as ccName
- ,cc.[Title] as title
- ,cc.[DirectPhone] as [group]
- ,cc.[EMail] as email
- from customers c
- full outer join customercontacts cc
- on c.customer collate Danish_Norwegian_CI_AS = cc.customer
- /*where c.customer = '10000011' */
- for xml auto, root('root')";
- SqlCommand com = new SqlCommand(CommandString, s);
-
- using(XmlReader x = com.ExecuteXmlReader())
- {
- XmlDocument document = new XmlDocument();
- document.Load(x);
- document.Save(context.Response.OutputStream);
- }
- }
- }
-
- public bool IsReusable {
- get {
- return false;
- }
- }
-
- public static string Compress(string text)
- {
- byte[] buffer = Encoding.UTF8.GetBytes(text);
- MemoryStream ms = new MemoryStream();
- using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
- {
- zip.Write(buffer, 0, buffer.Length);
- }
-
- ms.Position = 0;
- MemoryStream outStream = new MemoryStream();
-
- byte[] compressed = new byte[ms.Length];
- ms.Read(compressed, 0, compressed.Length);
-
- byte[] gzBuffer = new byte[compressed.Length + 4];
- System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
- System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
- return Convert.ToBase64String(gzBuffer);
- }
-
- public static string Decompress(string compressedText)
- {
- byte[] gzBuffer = Convert.FromBase64String(compressedText);
- using (MemoryStream ms = new MemoryStream())
- {
- int msgLength = BitConverter.ToInt32(gzBuffer, 0);
- ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
-
- byte[] buffer = new byte[msgLength];
-
- ms.Position = 0;
- using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
- {
- zip.Read(buffer, 0, buffer.Length);
- }
-
- return Encoding.UTF8.GetString(buffer);
- }
- }
-
-
- }