Problem with list inside list

Jul 12, 2012 at 10:52 PM
Edited Jul 13, 2012 at 4:10 AM

Hello Atul,

We have a problem with list inside list. Example: I´ve changed template 1 for show children of vendors.

The xml:

<field>
  <listFields>
    <field orderName="Test Order2" orderDescription="Test Order Description" id="" contentControlTagREFS="PlaceholderNonRecursiveC PlaceholderNonRecursiveD" />
    <field id="736d79d9-0689-419e-b17a-1867f613fd80" contentControlTagREFS="PlaceholderRecursiveA">
      <listFields>
        <field vendorName="Vendor 1" id="736d79d9-1689-419e-c27a-1867f613fd80" contentControlTagREFS="PlaceholderNonRecursiveA">
          <listFields>
            <field contentControlTagREFS="PlaceholderRecursiveC">
              <listFields>
                <field childName="Child 1.1" contentControlTagREFS="PlaceholderNonRecursiveE" />
                <field childName="Child 1.2" contentControlTagREFS="PlaceholderNonRecursiveE" />
                <field childName="Child 1.3" contentControlTagREFS="PlaceholderNonRecursiveE" />
              </listFields>
            </field>
          </listFields>
        </field>
        <field vendorName="Vendor 2" id="736d79d9-0589-419e-c27a-1867f613fd80" contentControlTagREFS="PlaceholderNonRecursiveA">
          <listFields>
            <field contentControlTagREFS="PlaceholderRecursiveC">
              <listFields>
                <field childName="Child 2.1" contentControlTagREFS="PlaceholderNonRecursiveE" />
                <field childName="Child 2.2" contentControlTagREFS="PlaceholderNonRecursiveE" />
                <field childName="Child 2.3" contentControlTagREFS="PlaceholderNonRecursiveE" />
              </listFields>
            </field>
          </listFields>
        </field>
      </listFields>
    </field>

    <field id="736d79d9-0689-119e-b27a-1867f613fd80" contentControlTagREFS="PlaceholderRecursiveB">
      <listFields>
        <field itemName="Item 1" id="736d79d9-0689-419e-c27a-1867f613fd80" contentControlTagREFS="PlaceholderNonRecursiveB" />
        <field itemName="Item 2" id="736d79d9-0689-419e-b27a-1867f613dd80" contentControlTagREFS="PlaceholderNonRecursiveB" />
      </listFields>
    </field>
  </listFields>
  <contentControls>
    <contentControl type="2" tag="PlaceholderNonRecursiveC" refControlValue="orderName" refTagValue="id" />
    <contentControl type="2" tag="PlaceholderNonRecursiveD" refControlValue="orderDescription" refTagValue="id" />
    <contentControl type="1" tag="PlaceholderRecursiveA" refTagValue="id" />
    <contentControl type="1" tag="PlaceholderRecursiveB" refTagValue="id" />
    <contentControl type="2" tag="PlaceholderNonRecursiveA" refControlValue="vendorName" refTagValue="id" />
    <contentControl type="2" tag="PlaceholderNonRecursiveB" refControlValue="itemName" refTagValue="id" />
    <contentControl type="1" tag="PlaceholderRecursiveC" />
    <contentControl type="2" tag="PlaceholderNonRecursiveE" refControlValue="childName" />
  </contentControls>
</field>

 The expected output would be:

Vendor Name: Vendor 1
Children:
Child name: Child 1.1
Child name: Child 1.2
Child name: Child 1.3
Vendor Name: Vendor 2
Children:
Child name: Child 2.1
Child name: Child 2.2
Child name: Child 2.3

 
However, the output is:
Vendor Name: Vendor 1

Children:
Child name: Child 1.1
Child name: Child 1.2
Child name: Child 1.3

Vendor Name:
Children:
Child name: Child 1.1
Child name: Child 1.2
Child name: Child 1.3

Vendor Name: 
Children:

Vendor Name: 
Children:

Vendor Name: 
Children:

Vendor Name: Vendor 2
Children:
Child name: Child 2.1
Child name: Child 2.2
Child name: Child 2.3

Vendor Name:
Children:
Child name: Child 2.1
Child name: Child 2.2
Child name: Child 2.3

Vendor Name: 
Children:

Vendor Name: 
Children:


Vendor Name: 
Children:

So, the elements are repeated. The recursive tag "PlaceHolderRecursiveA" has 10 field tags,
including vendor and children fields. So, the output shows, wrongly, 10 vendor name tags.
I will send for your email the template and  xml.
Do you know this problem?
Thanks,
Jorge
Coordinator
Jul 13, 2012 at 7:38 PM

I have fixed the issues and mailed you the changes.

 

Regards,

Atul

Jul 13, 2012 at 8:47 PM
Hi Atul,
Thanks for your reply.
The fix worked!!! :)
Regards,
Jorge
Apr 24, 2013 at 4:35 AM
Atul,
The fix has worked, so I will share your response:

In SampleGenericDocumentGeneratorUsingXml.cs:

The iterator was being moved over inner nodes hence extra information was showing up. I created the fix displayed below within RecursivePlaceholderFound method to restrict iterating over inner nodes. Basically I am checking the first ancestor and node is considered only if ancestor's attribute contains the place holder name. I have attached the updated file. Let me know if any changes are required. I will commit the same to CodePlex depending on your feedback.
// Get the Ancestors
                    XPathNodeIterator xIterator = nodeIterator.Current.SelectAncestors("field", (openXmlElementDataContext.DataContext as XmlNode).NamespaceURI, false);

                    // Consider only the first Ancestor of Node
                    if (xIterator.MoveNext())
                    {
                        // Get the attribute of the first Ancestor
                        string attr = xIterator.Current.GetAttribute("contentControlTagREFS", (openXmlElementDataContext.DataContext as XmlNode).NamespaceURI);

                        if (!string.IsNullOrEmpty(attr))
                        {
                            // If Ancestor attribute contains the current place holder then only clone element
                            if ((new List<string>(attr.Split(' '))).Contains(tagPlaceHolderValue))
                            {
                                XmlDocument e = new XmlDocument();
                                e.LoadXml(nodeIterator.Current.OuterXml);
                                SdtElement clonedElement = this.CloneElementAndSetContentInPlaceholders(new OpenXmlElementDataContext() { Element = openXmlElementDataContext.Element, DataContext = e.DocumentElement });
                            }
                        }
                    }