For the second group, you would need to refine your strategy:
if you encounter a <subsectn> where n is less than or equal to the previos subsection ... you would need to:
-- clear your list BUT
-- NOT output </section>
edit: you obviously have to output each line you read ... if there is no need to write an end tag, just output the line read ... when there is a need to write one or more end tag(s), write them and then output the line just read ... at
end of file, write the line just read before your end tags.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
gerrylowry
All-Star
20513 Points
5712 Posts
Re: how to put closing tag according to each opening tag
Feb 22, 2012 03:27 PM|LINK
@ v-ambily
i'm guessing that you already know how to write the c# ... you simply need a strategy ...
i'm assuming that each line you've shown us starts on a new line.
as per on your example, in pseudo-code, a strategy like this should work:
(a) your first line is an exception ... just read it and save it
(b) create a List<string> secitons
(c) every time you get a <subsectn> add it to your list ...
(c) every time you get a <seciton> tag, reverse process your list, that output </section>.
(d) at end of file, reverse process your list, that output </section>.
note: when you reverse process your list, delete each entry so that your list is empty as you begin processing a new section.
The above strategy works for your first group.
<subsect1>
<subsect2>
<subsect3>
<subsect4>
<subsect5>
For the second group, you would need to refine your strategy:
if you encounter a <subsectn> where n is less than or equal to the previos subsection ... you would need to:
-- clear your list BUT
-- NOT output </section>
<subsect3>
<subsect1> lower ... output </subsect3>
<subsect2>
<subsect1> lower ... output </subsect2>, </subsect1>
<subsect2>
<subsect3>
<subsect1> lower ... output </subsect3>, </subsect2>, </subsect1>
<subsect1> equal ... output </subsect1>
end of file ... output </subsect1>, </section>
g.
edit: you obviously have to output each line you read ... if there is no need to write an end tag, just output the line read ... when there is a need to write one or more end tag(s), write them and then output the line just read ... at end of file, write the line just read before your end tags.