• 0

Tricks and Tips for storing html in xml


Question

Hi everyone. I'm using C# to create xml files of data and I've never had a problem doing what I've been doing until I incorporated a new type of data to include, which is an html description block. Once I did this all hell broke loose.

Is there a guide somewhere to like a set of tricks to make sure your html is xml safe so you can do something like <MyElement Description="my html description here" />. B/c I keep running into road blocks.

Thanks :)

9 answers to this question

Recommended Posts

  • 0

You could surround the HTML string in a CDATA string, i.e. <![CDATA[ my_html_description ]]> However if the html contains a CDATA block then it will be messed up.

You could also scan through your HTML replacing special XML characters with their equivalent XML escape code:

" to "

< to <

> to >

& to &

Obviously you'll need to reverse this process when you remove the HTML.

  • 0

^ Base64 encoding is probably a little more complicated than simply having the description as its own element:

&lt;MyElement&gt;
  &lt;description&gt;
    &lt;p&gt;Here is a &lt;strong&gt;formatted&lt;/strong&gt; description&lt;/p&gt;
  &lt;/description&gt;
&lt;/MyElement&gt;

If you enforce an xhtml syntax on your descriptions, that way you can ensure that the html description is semantically strong. There are downisdes to that, in that if the html is not well formed, the whole xml stream will encounter errors whilst being parsed. You need to decide your risks for each design.

  • 0

Thanks for all your help so far. I don't know why base64 never occurred to me though I just added 2 static functions in my program that change the code although it might be more efficient to base64 encode it when coming to large descriptions come to think of it.

Thanks to all of you you've all given me something to think about, either way you all provided answers. I would do it the way you said Antaris but then since the description is put in manually by the end-user I have no guarantee that it's well-formed. I tested it that

way and ran into hundreds of parsing errors.

  • 0

The best solution of course is to use a tag soup parser to construct a DOM, then parse it back out again (valid HTML will be 1:1, invalid HTML will come out valid)

Failing that, storing it as CDATA would be the next best (assuming you can't enforce XHTML, which is quite possible)

  • 0
  On 10/03/2010 at 17:31, The_Decryptor said:

The best solution of course is to use a tag soup parser to construct a DOM, then parse it back out again (valid HTML will be 1:1, invalid HTML will come out valid)

Failing that, storing it as CDATA would be the next best (assuming you can't enforce XHTML, which is quite possible)

This. ;)

  • 0
  On 10/03/2010 at 17:17, Rob said:

Part of XML's charm is that it is human-readable, to an extent. Base64 encoding it removes that.

He didn't say that it needed "charm." Why go though the pain and pitfalls of a tag parser if you don't need it? If all he's doing is storing the HTML in the XML attribute, I still think encoding is the best idea because it's the simplest and would reduce to zero the chance of screwing up his XML. If he needs validation of the HTML, he can plug that in after the decoding step.
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Lifetime subscription to Mail Backup X gets price dropped by 72% by Steven Parker Today's highlighted deal comes via our Apps + Software section of the Neowin Deals store, where you can save 72% off a lifetime subscription to Mail Backup X Individual. For most individuals and organizations, emails are the most critical part of daily activities and communications. Some of us realize the importance of backing up emails only when critical emails are lost for some reason. Plan ahead and safeguard your mail data today with a robust and reliable mail backup solution. Mail Backup X is a one-stop solution for mail backup, archiving, email management & mail conversion trusted by 42,000+ business and home users worldwide. Backup from major mail clients. Apple Mail, Microsoft Outlook, Office 365, Microsoft Exchange, Thunderbird, Postbox Backup from mail services. Gmail, Outlook.com, Yahoo, Gmx.de, Office365, Microsoft Exchange, or any service supporting IMAP protocol Archive file viewer. Quickly search & view your emails from archives Highly compressed archives. Save up to 3x storage space Import almost any mail archive. Files like .pst, .ost, .mbox, .olk, .eml, .rge, and more Mirror backup. Cloud storage (Google Drive, One Drive, Dropbox, Pcloud or FTP) or USB drive Restore. Restore direct to the server account or a separate server account Migration. Move all mails onto a new account in Office365 100% privacy. Encrypt & secure your data with military-grade aes 256-bit encryption and your own private key, so it's only visible to you Top-notch premium support. Get help that you need from experts Good to know Plan: Individual Edition Length of access: lifetime Redemption deadline: redeem your code within 30 days of purchase Access options: desktop Max number of devices: 2 Only available to new users Version: 2 Updates included A lifetime subscription to Mail Backup X normally costs $179, but you can pick this up for just $49.99 for a limited time - that represents a saving of $129 (72% off). For a full description, spec, and terms, click the link below. Get Mail Backup X (lifetime plan) for just $49.99 (was $179) Use coupon code SAVE20 at checkout to get this product for an additional 20% off We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • I will believe it when it happens. iOS 18 was heavily rumoured to be a massive overhaul with visionOS glass style UI elements. Never happened. I don't even believe the x26 naming scheme is real either, feels more like an April fools joke *shrugs* I'll be happy to be proven wrong. However, till Apple themselves say it's so I will remain skeptical.
    • Nothing Phone (2a) hits a sweet spot: Is this mid-ranger the best value for your money? by Paul Hill Are you in the UK and looking for a smartphone with a standout design, solid display, good battery life, and smooth software? Then the Nothing Phone (2a) stands out as a solid mid-range option, especially considering it’s discounted 36% from £349 to £222. To clarify, this is the 12GB+256GB Nothing Phone (2a), not the weaker 8GB+128GB model. I’ll cover what’s included with this phone, but if you’re convinced to pick it up, be aware that this is marked as a limited-time deal so don’t sit on your hands. What the Nothing Phone (2a) delivers (and where it stands out) Powering the Nothing Phone (2a) is a mid-range Mediatek Dimensity 7200 Pro processor, 12GB of RAM with an 8GB RAM booster, and an advanced cooling system to boost game performance and longevity. In terms of battery life, the Nothing Phone (2a) packs a 5,000 mAh battery that will keep the device running for up to two days. When the battery does drain, you have 45W fast charging which will take 40 minutes to get you to 100% battery. On the back of the phone there is a 50 MP Main (OIS & EIS) camera as well as a 50 MP ultra-wide camera. On the front, you get a 32 MP camera. This phone tries to punch above its weight with the camera with features like Ultra XDR and Portrait Optimiser. The display is pretty good too, it includes a 6.7-inch AMOLED with 1,300 nits peak brightness and 120Hz adaptive refresh rate. This screen is ideal for gaming with its higher refresh rate and that cooling system I mentioned earlier will help it keep chugging along nicely. This phone came out in the first half of 2024, so you may be wondering what the software situation is. It initially shipped with Nothing OS 2.5 which is powered by Android 14, but this has since been upgraded to Nothing OS 3.0, powered by Android 15. Nothing has promised three major OS upgrades for this device, but one has been used up so expect Nothing OS releases based on Android 16 and Android 17 in the coming years. For those not familiar with Nothing OS, it has a strong reputation for being clean, smooth, and well-optimised, with little to no bloatware. If you’re coming from a brand like Xiaomi which packs its devices with bloatware and even ads, then the Nothing Phone (2a) should feel very clean. Finally, environmentalism has come more into focus over the last decade and while buying a second hand phone is best, Nothing has made decent efforts with this device by using recycled materials to keep the carbon footprint low. Should you buy it? At £222, the Nothing Phone (2a) is significantly reduced in price, making it a solid option for people looking for a mid-range device. If you like the unique design, clean software, good camera, and a strong battery life, then this device is definitely for you. The deal is marked as a limited-time deal and Amazon has marked it as an Amazon Choice suggesting that it’s a good phone to buy. It’s also at its lowest price, so you definitely aren’t paying over the odds. If you’re convinced, check the buying link below! Nothing Phone (2a): £222 (36% off) / MSRP: £349 This Amazon deal is U.K. specific, and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon UK deals page here. Get Prime, Prime Video, Music Unlimited, Audible or Kindle Unlimited, free for the first 30 days As an Amazon Associate we earn from qualifying purchases.
    • "It will have curved glass sides around the entire phone, even at the edges." Will it be just a collectible? I can't imagine myself having a glass phone. I'd gladly exchange glass back panel for plastic or aluminium on mine.
  • Recent Achievements

    • Week One Done
      Al_ earned a badge
      Week One Done
    • Week One Done
      MadMung0 earned a badge
      Week One Done
    • Reacting Well
      BlakeBringer earned a badge
      Reacting Well
    • Reacting Well
      Lazy_Placeholder earned a badge
      Reacting Well
    • Dedicated
      Epaminombas earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      477
    2. 2
      +FloatingFatMan
      274
    3. 3
      ATLien_0
      243
    4. 4
      snowy owl
      208
    5. 5
      Edouard
      182
  • Tell a friend

    Love Neowin? Tell a friend!