Search code examples
c#xmlstring-length

How to reduce XML string length for storing in DB?


I have XLM that looks like that (length 278 characters):

<AppList>
  <App>
    <Info1>Value 1</Info1>
    <Info2>Value 2</Info2>
    <Info3>Value 3</Info3>
    <AppList>
      <App>
        <Info1>Value 4</Info1>
        <Info2>Value 5</Info2>
        <Info3>Value 6</Info3>
      </App>
    </AppList>
  </App>
</AppList>

For storing in DB I want it to look like that (length 192 characters):

<AppList><App><Info1>Value 1</Info1><Info2>Value 2</Info2><Info3>Value 3</Info3><AppList><App><Info1>Value 4</Info1><Info2>Value 5</Info2><Info3>Value 6</Info3></App></AppList></App></AppList>

In Notepad++ they have something like Linearize XML, which does the work.
Is there any way to do that programmatically in C#?


Solution

  • You can consider below methods.

    1. With reference to the duplicated content, you can do it in C#.

    2. Just Compress text-to-text: there's several methods to compress texts. Of course, it is hard to do string search afterwards.

    3. Use XML support in SQL: most of SQL database stores XML in compressed manner natively. Or this article could be helpful also.

    4. Describe in Json: XML is verbose than json expression in general. XML and json both are tree structure so 1:1 conversion is possible.