Adding Nodes - InfoPath Dev
in

InfoPath Dev

Adding Nodes

Last post 03-30-2009 10:05 AM by ben.oates. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 03-02-2009 09:52 AM

    Adding Nodes

    Hi all,

    I am literally teraring hair out as I have been googling and attempting numerous variations of this code for quite some time now.

    If I createElement, I get this error:
    Element 'UID' is unexpected according to content model of parent element '{http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-08T16:31:56}Order'

    If I createNode, I get the same error but with the curly brackets blx before UID as well. The strName I am passing is UID (although I have tried my:UID as well). my:Order is repeating and contains a repeating field called UID. I pass a single Order node as objParentNode using this code (extracted from more code):

    Set iNode = XDocument.Dom.selectNodes("/my:myFields/my:Order")
    Set iNode = iNode.item(0)
    addDomElement iNode, "UID", ""

    And the addDomElement Sub looks like this (again it's been through many iterations and none have worked):

    Sub addDomElement(objParentNode, strName, strValue)
       'Set objNode = XDocument.Dom.createElement(strName)
      
    Set objNode = XDocument.Dom.createNode("element", strName, http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-08T16:31:56)

       If strValue <> "" Then objNode.Text = strValue

       Set existingObjNode = objParentNode.selectNodes(strName)

       objParentNode.appendChild objNode

    End Sub

    Please tell me where I am going wrong with this.

  • 03-02-2009 08:48 PM In reply to

    Given what I can discern of your data structure, I'd bet that 'UID' should be 'my:UID'. Do you get the same error with that?

    Hilary Stoupa

  • 03-03-2009 02:52 AM In reply to

    Hi Hilary,

    I do I'm afraid, except it says that {http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-08T16:31:56}UID is unexpected.

    I also tried the following:

    Sub addDomElement(objParentNode, strName, strValue)

    Set existingObjNode = objParentNode.selectNodes(strName)

    If existingObjNode.length > 0 Then

    Set existingObjNode = existingObjNode.item(existingObjNode.length -1)
    objParentNode.appendChild existingObjNode

    Else

    Set objNode = XDocument.Dom.createNode("element", strName, http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-08T16:31:56)
    If strValue <> "" Then objNode.Text = strValue
    objParentNode.appendChild objNode

    End If

    End Sub

    When I try the objParentNode.appendChild existingObjNode line I get the following error:

    {http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-08T16:31:56}Info is unexpected.

    my:Info is another, non-repeating child of Order. Given the code above I have no idea why it is even being selected! objParentNode is my:Order. In the second line I'm selecting the repeating my:UID nodes into existingObjNode then trying to set the last one of those into existingObjNode which should be a my:UID.

    Very confused.

  • 03-03-2009 08:15 AM In reply to

    Just a thought...

    Could it be because the system is trying to add the object in the wrong place? It wouldn't make sense to me really, but it's the only thing I can think of.

    <my:Order return="false">
       <my:HWID />
       <my:SWID />
       <my:UID />
       <my:Info />
                     <-- I think it's trying to insert <my:UID /> here
    </my:Order>

    The first 3 are repeating and when I come to run this set of processes I don't mind removing the my:Order that's there and starting from scratch if necessary... assuming this is the problem.

    How would I go about that though?

  • 03-03-2009 01:01 PM In reply to

    Perhaps we should look a little higher level -- what exactly do you need to achieve with your code? Are you creating a new 'Order' node?

    Hilary Stoupa

  • 03-30-2009 05:59 AM In reply to

    Ok - this is the basic path users take. Please forgive me if I cover any unnecessary ground or miss anything.

    At the first screen, they choose the hardware that they want from a data connection which is entered into a repeating list with an identifier called HID - it has various other fields that are mostly pulled from the data connection.

    In the second screen the user does the same but for a software list with SID

    In the third screen, they can add users from a data connection to a user database. Again, it is repeating with a unique identifier UNetID.

    When they come to the order screen, there is a script that I want to run that automatically populates the first Order group (assuming it has not been previously populated) with the ID numbers from each of these previous fields.

    The reason I have a repeating Order group is because on occasion people will put in multiple orders in a single form (i.e. everyone wants this hardware but only 3 people want the software on top so the software and those people are bunged in a seperate order at the bottom).

    The problem I am having is when we come to the Order page.

    <my:Order return="false">
       <my:HWID />
       <my:SWID />
       <my:UID />
       <my:Info />
                     <-- I think it's trying to insert <my:UID /> here
    </my:Order>

    The system is happy to copy across one user, one software item and one hardware item, but as soon as I try to add another of any of them (they are repeating elements - info is not) it throws the error described above.

    I am wondering if the Order is empty, should I delete it and start from scratch with nothing then add Order back in to the parent element my:myFields. This is pretty much the only thing I can think of that I haven't tried but I'd be grateful for any other suggestions in the mean time.

    For anyone else looking at this, I have tried XDocument.View.ExecuteAction("xCollection::insert", "UID_4") and get an "unspecified error" but I know the group is named properly. For some reason it just doesn't want to add a node!

  • 03-30-2009 07:22 AM In reply to

    The ExecuteAction approach to adding nodes does not work for repeating sections that are inside other repeating sections and I'm assuming my:Order is repeating and that's why you're getting the unspecified error. I also think you are correct in surmising that InfoPath is trying to insert my:UID after my:Info and that is why you are getting the schema error.

    As a design suggestion, it is usually simpler to have non-repeating nodes to group repeating nodes of the same type, so instead of what you have now, it would be:

    <my:Order return="false">
       <my:HWIDs>
          <my:HWID/>
          ...
       </my:HWIDs>
       <my:SWIDs>
       ...
       </my:SWIDs>
       <my:UIDs>
          <my:UID/>
       ...
       </my:UIDs>
       <my:Info />
    </my:Order>

     That makes it much easier to locate the node where you want to add new nodes.  It's not impossible to work with what you have, but it's considerably trickier, so if it's not too late in the game to change your data source I would recommend doing so.  If that's not an option, we can look at a code approach to work with the data source you have.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 03-30-2009 07:59 AM In reply to

    Hi Jimmy,

    That's a great idea and I will certainly use it in my next form, but this form has already been used for thousands of submissions and is parsed by hundreds of lines of code in several locations so making a change like that would not be possible.

    As there are elements after my:Order in my:myFields I am concerned that deleting the my:Order entry and trying to insert one again will probably yield the same issue. Also, the schema states that my:Order\my:UID count is > 0 - would code be able to circumvent (ignore) this problem to remove all the elements and push new ones in?

    I really can't think of anything else to do with this form. Any suggestions would be greatly appreciated.

  • 03-30-2009 08:58 AM In reply to

    Ok, fair enough.  Please try the following code:

    Set firstOrderNode = XDocument.DOM.selectSingleNode("/my:myFields/my:Order")

    Set newNode = XDocument.DOM.createNode("element", "my:UID", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-03-30T14:13:12")

    Set nextNode = firstOrderNode.selectSingleNode("my:Info")

    firstOrderNode.insertBefore newNode, nextNode

    The task will be a bit harder if you intend to insert SWIDs and HWIDs in the same fashion.  Please let me know if that is the case.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 03-30-2009 10:05 AM In reply to

    Jimmy, you're a legend. I adopted it to match my code and made all the relevant changes for missing SWID elements etc. and it works a treat.

    Thanks very much.

    I can't believe I missed insertBefore!

Page 1 of 1 (10 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.