Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Closed Thread
Very easy way to XML your data?
Old 09-24-2008, 03:40 PM Very easy way to XML your data?
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
The easiest way is just to tack a FOR XML AUTO onto the end of a SELECT query. But that isn't always what you want. Sometimes you want a fragment, a single data value wrapped up into a tag.

The trouble is, that data might contain a " or a & or worse, a < or > character. If you try SELECT '<Column1>' + Column1 + '</Column1>' sooner or later you'll generate malformed XML, that won't parse. This is the great thing about XML AUTO, it does the XML version of UrlEncode on all your data. But it generates a complete document. You could shred the output, but there's an easier way.

A blog called System.Reflection.Emit has published a great tutorial on how to do this. It involves writing a very simple SQL CLR function (at least, I'd recommend deploying it as a scalar function) that makes short work of obeying the rules of XML, and producing fragments.

Code:
namespace XmlUtil { private static XmlDocument _staticDoc = null; private static StringWriter _staticStringWriter = null; private static XmlWriter _staticXmlWriter = null; /// <summary>Converts Unicode text into ASCII-compliant XML encoded text</summary> public static string EncodeText(string str) { if (str == null) return ""; if (_staticDoc == null) { _staticDoc = new System.Xml.XmlDocument(); _staticDoc.LoadXml("<text></text>"); _staticStringWriter = new StringWriter(); XmlWriterSettings settings = new XmlWriterSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; _staticXmlWriter = XmlTextWriter.Create(_staticStringWriter, settings); } lock (_staticDoc) { _staticDoc.LastChild.InnerText = str; str = _staticDoc.LastChild.InnerXml; } // ASCII enforcement StringBuilder sb = new StringBuilder(); char[] chars = str.ToCharArray(); for (int i = 0; i < chars.Length; i++) { char c = chars[i]; if ((int)c > 127) // goes beyond ASCII charset { lock (_staticStringWriter) { lock (_staticXmlWriter) { _staticXmlWriter.WriteCharEntity(c); _staticXmlWriter.Flush(); StringBuilder _sb = _staticStringWriter.GetStringBuilder(); sb.Append(_sb.ToString()); _sb.Length = 0; } } } else sb.Append(c); } return sb.ToString(); } }


__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
View Public Profile
 
 
Register now for full access!
Closed Thread     « Reply to Very easy way to XML your data?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13607 seconds with 12 queries