Question

I am working on a project in C# (vs2005) that takes XML and creates a docx file. (When I say XML, I mean well formatted HTML, and I am reading it with an xmlreader).

In a scenario, I may be given a list within a list within a list etc... I need to be able to identify which list (by giving it a unique number) a piece of text is in, without confusing it with lower level lists or a separate list later in the file, at the same level.

Below is a simple example of my routine, and is not actually the code that I have. Don't bother optimizing this code.

int LoopCounter = 0;
int LoopTotal = 0;
void ParseXmlNode(XmlNode Node)
{
    switch (Node.Name)
    {
        case "ol";
            ++LoopCounter;
            ++LoopTotal;
            break;
        case "#text":
            //This is some text within a list.
            int ListIdentifier = 0;
            //How do I calculate the ListIdentifier?
            break;
    }
    foreach(XmlNode childNode in Node.ChildNodes)
    {
        ParseXmlNode(childNode);
    }
    switch (Node.Name)
    {
        case "ol";
            --LoopCounter;
            break;
    }
}

Assuming that I am given the type of Xml below,

<ol>
    Text in list #1.
    <ol>
        Text in list #2.
    </ol>
    Text still in list #1.
</ol>

How would I assign a unique number to the text during the parsing of each text item?

The reason for calculating the identifier during the parsing of the text is so that I can create a paragraph in the docx file that corresponds to the right numbering pattern. Each list is going to be given its own UUID basically, and in order for the text to be numbered in the right order, I need to calculate the UUID while parsing the text, to tell it which list it belongs to.

If I need to specify any more information, please do not hesitate to ask! Thanks for the help!

For clarification, my idea is to have each text node within a list have an integer assigned, common with all other text nodes in that same list. In any other list, I cannot have the same number. I cannot change the recursive routine that I have.

Was it helpful?

Solution

Do you need something like this:

Random rnd = new Random();
List<int> list = new List<int>();
Stack<int> listStack = new Stack<int>();

public void Main()
{
    int listNumber = GetUniqueRandomNumber();
    // do your parsing stuff
    // ...

    // When an <ol> is found
    listStack.Push(listNumber);
    listNumber = GetUniqueRandomNumber();

    // Whenever #text is encountered
    ListIdentifier = listNumber;

    // Whenever </ol> is found
    listNumber = listStack.Pop();
}

public int GetUniqueRandomNumber()
{
    int num = 0;

    while (num == 0)
    {
        num = rnd.Next(1, 5000);
        if (list.Contains(num))
            num = 0;
        else list.Add(num);
    }
    return num;
}

So what this is doing is its got a stack that will generate a new unique number everytime an <ol> is encountered and all text under that can be assigned to that unique number. Whenever a </ol> is encountered, it pops back to the previous number assigned to the previous list. As long as the <ol> and </ol> are balanced, this should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top