0

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#?

Gabriel
  • 377
  • 1
  • 3
  • 15
  • 2
    Possible duplicate of [C#/XSLT: Linearizing XML partially working code](http://stackoverflow.com/questions/15131542/c-xslt-linearizing-xml-partially-working-code) – Mohit S Jan 10 '17 at 07:20
  • @MohitShrivastava the suggested duplicate has no good answer – Toshi Jan 10 '17 at 07:24
  • `Regex.Replace(xmlString, @">\s+<", "><")`. There may be some edge cases that could be problematic. – Jason Boyd Jan 10 '17 at 07:27
  • Removing `Line-feeds` is not an option? `xmlString = xmlString.Replace(System.Environment.NewLine, xmlString)` – Smartis has left SO again Jan 10 '17 at 07:31
  • 1
    On a side note: it doesn't mean because you can store xml (or other serialized objects) in a relational database that you should. Always ask yourself what the added benefit is. Serialization / deserialization of relational data isn't overly expensive these days. – Wim Ombelets Jan 10 '17 at 07:49
  • Read it calmly @Toshi Even the partial working code is nicer and OP can improve it to make it more appropriate. – Mohit S Jan 10 '17 at 08:07

1 Answers1

2

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.

Community
  • 1
  • 1
Youngjae
  • 24,352
  • 18
  • 113
  • 198