Search code examples
.netvb.netbbcode

convert BBcode to Json or xml


my question is very simple there a way to conver BBCODE to XML or JSON ? i have a db that have a fild that contain a BBCODE that will be send to printer. i need to convert this bbcode into a valid json or xml to be easy parse it . an exaple of BBCODE

   [area][center] ΑΝΤΙΤΥΠΟ - ΟΧΙ ΠΡΟΣ ΠΩΛΗΣΗ[/center][/area]
   [area name="Pilot"][/area]
   [area width=100  x=20 name="Opap" parentName="Pilot" scale="0.85"][center][bitmap filename="C:\Users\Agent\AppData\Local\INLOT\Lottery\resources\logos\BW_01_OPAPv.jpeg"][/bitmap][/center][/area]
   [area width=350  x=130 name="Logo" parentName="Pilot"][center][bitmap filename="C:\Users\Agent\AppData\Local\INLOT\Lottery\resources\logos\BW_01_KINO.jpeg"][/bitmap][/center][/area]
   [area x=450  width=150 name="Barcode" parentName="Pilot" height=100][right][barcode scale="0.8" format=graphics type=QR value=""][/barcode][/right][/area]
   [area y=10 name="Barcode_Text" parentName="Logo"][center][b][/b][/center][center][/center][/area]
   [area y=5 name="headerSeparator" parentName="Barcode_Text"][line x1=15 y1=5 x2=600 y2=5 penWidth=2][/area]

   [area y=5 x=15 name="boardSystem0" width=120 parentName="headerSeparator"][o y=5]:[/o][/area]
   [area y=5 x=130 name="nums0" width=560 parentName="headerSeparator"][o y=5] -- -- -- -- -- -- -- -- -- -- -- --[/o][/area]
   [area y=5 x=15 name="multiplier0" parentName="nums0"][o y=5][/o] ΠΟΛ/ΣΤΗΣ: 1 KINO BONUS: ΟΧΙ [/area]
   [area y=5 name="footerSeparator0" parentName="multiplier0"][line x1=15 y1=5 x2=595 y2=5 penWidth=2][/area]
   [area y=5 x=15 name="oddeven" parentName="headerSeparator"][o y=5]ΜΟΝΑ/ΖΥΓΑ: ΜΟΝΑ[/o][/area]
   [area y=5 x=15 name="multiEvenOdd" parentName="oddeven"]ΠΟΛ/ΣΤΗΣ: 2 (ΠΟΣΟ: €1,00)[/area]
   [area x=12 y=10 width=600 name="evenSeparator" parentName="multiEvenOdd"][center]-----------------------------------------------------------[/center][/area]
   [area y=5 x=15 name="sideColumns" parentName="evenSeparator"][o y=5]ΣΤΗΛΕΣ: 1H -- -- -- -- -- -- -- -- -- --[/o][/area]
   [area y=5 x=15 name="multiColumns" parentName="sideColumns"]ΠΟΛ/ΣΤΗΣ: 100 (ΠΟΣΟ: €50,00)[/area]
   [area x=12 y=10 width=600 name="columnSeparator" parentName="multiColumns"][center]-----------------------------------------------------------[/center][/area]


   [area y=10 x=24 name="amounts" width=300 parentName="columnSeparator"][left][size height=2][b]ΣΥΝΟΛΙΚΗ ΑΞΙΑ ΔΕΛΤΙΟΥ:[/b][/size][/left][/area]
   [area y=10 x=300 name="amounts" width=300 parentName="columnSeparator"][right][size height=2][b]€51,00[/b][/size][/right][/area]
   [area y=5 x=24 name="draws" width=300 parentName="amounts"][left]ΣΤΗΛΕΣ: 102[/left][/area]
   [area y=5 x=300 name="draws" width=300 parentName="amounts"][right]ΚΛΗΡΩΣΕΙΣ: 1[/right][/area]
   [area x=24 name="drawFrom" width=300 parentName="draws"][left]ΑΠΟ: 256489[/left][/area]
   [area x=300 name="drawFrom" width=300 parentName="draws"][right](17/06/2020 15:50:00)[/right][/area]
   [area x=24 name="drawTo" width=300 parentName="drawFrom"][left]ΕΩΣ: 256489[/left][/area][area x=300 name="drawTo" width=300 parentName="drawFrom"][right](17/06/2020 15:50:00)[/right][/area]
   [area x=24 name="currentDraw" width=300 parentName="drawTo"][left]ΚΛ/ΚΑΤ: 256489[/left][/area]
   [area name="Footer" parentName="currentDraw"][line x1=12 y1=5 x2=600 y2=5 penWidth=2][o y=5][center]ΤΥΠΩΘΗΚΕ 17/06/2020 15:45:42[/center][/o][center]ΠΡΑΚΤΟΡΕΙΟ: 234019-234019001 23401981[/center][center]ΚΩΔ. ΣΥΝ.:c65fc8[/center][center]ΚΩΔ. ΔΕΛΤΙΟΥ.:100003510257[/center][center][barcode format=graphics type=PDF value="289354170400000058881275342376010000025667000002629905159"][/barcode][/center][/area]
   [area][center] ΑΝΤΙΤΥΠΟ - ΟΧΙ ΠΡΟΣ ΠΩΛΗΣΗ[/center][/area]
[/page]

Solution

  • I agree with tgolisch's comment and would try this:

    Dim htmlStr = "<html><body>" & bbStr.Replace("<","&lt;") _
      .Replace(">","&gt;") _
      .Replace("[","<") _
      .Replace("]",">") & "</body></html>"
    

    htmlStr now contains a block of something like html and you could look at parsing it with html agility pack, a parser for html that tolerates badly formed html - bbcode is intended to be pseudo html and is not guaranteed to be well formed. XML might well choke on your bbcode. HAP supports custom tags that are not html standard as far as I know. It refers to tags as lowercase all the time so if some of your tags are upper or mixed case you should use lowercase when building xpath expressions to extract the data