Home > Blockchain >  take raw formatted text from xml and separate separate lines
take raw formatted text from xml and separate separate lines

Time:11-09

A customer added data into a text area and separated line items by pressing enter and put each item on a separate line. In the XML file, each item is on a separate line, but when I render the page, the text all comes together on one line. Is there a way to take each separate line and add it to a list, then make the following:

    Example
<description>
    Panel Style: Full-View
    R-Value:  None
    Sectional Material:  Heavy Duty/Aluminum
    Insulation Type:  No Insulation
    Color Options:  Aluminum, White, Bronze, Black
</description>

Would become:
<description>
    <ul>
        <li>Panel Style:  Full-View</li>
        <li>R-Value:  None</li>
        <li>Sectional Material:  Heavy Duty/Aluminum</li>
        <li>Insulation Type:  No Insulation</li>
        <li>Color Options:  Aluminum, White, Bronze, Black</li>
    </ul>
</description>

CodePudding user response:

I figured it out. I'm going to post the code just in case someone else runs into this.

foreach (var r in editableboxgroup.boxes)
{
    var array = r.description.Split('\n');
    var item = "<ul>";

    foreach(var x in array)
    {
        item  = "<li>"   x   "</li>";
    }

    item  = "</ul>";
}

CodePudding user response:

Using Xml Linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication49
{
    
    class Program
    {
        const string INPUT_FILENAME = @"c:\temp\test.xml";
        const string OUTPUT_FILENAME = @"c:\temp\test1.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(INPUT_FILENAME);
            List<XElement> descriptions = doc.Descendants("description").ToList();
            foreach (XElement description in descriptions)
            {
                string innerText = (string)description;
                StringReader reader = new StringReader(innerText);
                string line = "";
                while ((line = reader.ReadLine()) != null)
                {
                    line = line.Trim();
                    if (line.Length > 0)
                    {
                        description.Add(new XElement("li", line));
                    }
                }
            }
            doc.Save(OUTPUT_FILENAME);

        }
    }
}
  • Related