0

I have a requirement to build a string by taking html content as xml and display as output below , I can achieve below output but spacing at different hierarchy level were missing. Someone please help me in figuring above issue

Input

<xml>
  <Test1>
    <Test1-Sub1>
      Testing 1 sub 1
    </Test1-Sub1>
    <Test1-Sub2>
      <Test1-Sub2-SubPart1>
        Testing Sub2-SubPart1
      </Test1-Sub2-SubPart1>

    </Test1-Sub2>
  </Test1>
  <Test2>
    <Test2-Sub1>
      <Test2-Sub1-SubPart1>
        Testing Sub1-SubPart1
      </Test2-Sub1-SubPart1>
     </Test2-Sub1>
  </test2>
</xml>

Output

xml
  test1
    test1-sub1
      | Testing 1 sub 1
    test1-sub2
      test1-sub2-subpart1
        | Testing Sub2-SubPart1
  test2
    test2-sub1
      test2-sub1-subpart1
        | Testing Sub1-SubPart1

Generic solution would really helpful..

  • 4
    Show your code. It's unlikely a random person on the Internet is going to write your program for you, but you might get help fixing a bug if the code sample you share is minimal but complete. – adv12 Dec 18 '15 at 19:55
  • if you are parsing xml recursively look at the following website : http://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Dec 18 '15 at 20:10
  • @jdweng Thanks for the reply I have achieved the tree view but falied tp get spacing at different hierarchy levels as shown in expected o/p – user5696437 Dec 18 '15 at 20:15
  • You need to pass into the recursive function a 2nd parameter 'level' which you increment every time you enter the recursive function. Your indenting will use the level parameter to increase the number of spaces at beginning of the line. – jdweng Dec 18 '15 at 20:46

1 Answers1

1

Following class will help you to do your stuff.

class XmlHierarchicalTextBuilder
{
    private XmlDocument xmlDoc;
    private string hierarchicalText;
    private int indentlevel = 0;

    private const string NEW_LINE = "\n";
    private const string TAB = "\t";
    private const string PIPE = "|";
    private const string SPACE = " ";

    public XmlHierarchicalTextBuilder (string xml)
    {
        xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml); 
    }

    public string Build()
    {
        Build(xmlDoc.DocumentElement);
        return hierarchicalText;
    }

    private void Build(XmlElement xmlElement) 
    {            
        if (xmlElement.HasChildNodes)
        {
            HandleXmlElementName(xmlElement);

            foreach (XmlNode item in xmlElement.ChildNodes)  
            {
                indentlevel++;

                if (item.NodeType == XmlNodeType.Text)
                {
                    HandleXmlElementInnerText(xmlElement);                      
                }
                else if (item.NodeType == XmlNodeType.Element)
                {
                    Build((XmlElement)item);
                }
                //add more conditions based on different xml node types in future, if u want

                indentlevel--;
            }

        }           
    }

    private void HandleXmlElementInnerText(XmlElement xmlElement)
    {
        addIndent();
        hierarchicalText +=  PIPE + SPACE + xmlElement.InnerText.Trim();
        addNewLine(); 
    }

    private void HandleXmlElementName(XmlElement xmlElement)
    {
        addIndent();
        hierarchicalText += xmlElement.Name;
        addNewLine();
    }

    private void addIndent()
    {
        for (int i = 0; i < indentlevel; i++)
        {
            hierarchicalText += TAB;
        }
    }

    private void addNewLine()
    {
        hierarchicalText += NEW_LINE;
    }
}

Pass your XML string to class constructor and call Build() method. It will return the hierarchical text what you required as an output.

Will appreciate if someone provide feedback and comments about the class I have designed. Hope that class can be altered in a better way.

Update : Solution

--SJ

codeninja.sj
  • 3,452
  • 1
  • 20
  • 37