Managed Code XPathNavigator - InfoPath Dev
in

InfoPath Dev

Managed Code XPathNavigator

Last post 04-03-2007 10:28 AM by Greg Collins. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 03-18-2007 07:47 AM

    • BhailDa
    • Not Ranked
    • Joined on 10-06-2006
    • United Kingdom
    • Posts 13

    Managed Code XPathNavigator

    I seem to keep getting errors when trying to assign a value to a field within InfoPath. If the field alreday has a value then no problem. but if its empty/null, then cann assign a value although Xpath seems to find it. Thoughts/views.?

    Code:

    // Create XPathNavigator positioned at the root of the main data
    // source.
    XPathNavigator myNav = this.MainDataSource.CreateNavigator()

    // Select employee field and set value to username.
    myNav.SelectSingleNode("//my:employee",this.NamespaceManager).
    SetValue(System.Environment.UserName);
  • 03-19-2007 09:54 AM In reply to

    Re: Managed Code XPathNavigator

    Is the issue that the node is "blank" or the node is "not present" because that is a big difference.

    Blank nodes should not pose any problem to you, and setting their value should work fine. However, null nodes (i.e. not present in the DOM) this is a "by design" issue. You can't set the value of a node that doesn't exist. First you must insert the node, then you can set its value.

    From what you've stated so far, it sounds like you have an optional field or section in the view, that hasn't been inserted yet. If so, again, this is by design. First insert the field/section, then set the value.
  • 04-03-2007 05:04 AM In reply to

    • BhailDa
    • Not Ranked
    • Joined on 10-06-2006
    • United Kingdom
    • Posts 13

    Re: Managed Code XPathNavigator

    From further investigation I found a solution. Simply check the node value before assignment as follows:


    // Insert Date
    DeleteNil(xnNewRow.SelectSingleNode("my:Today", this.NamespaceManager));
    xnNewRow.SelectSingleNode("my:Today", this.NamespaceManager).SetValue(formatDate(DateTime.Now, false));
    _____________________________________________
    Routine
    _____________________________________________
    #region Utils
    public void DeleteNil(XPathNavigator node)
    {
    if (node.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
    node.DeleteSelf();
    }
    #endregion
  • 04-03-2007 10:28 AM In reply to

    Re: Managed Code XPathNavigator

    Yes, the nil attribute MUST be removed from a node prior to adding a value. Inserting a value into a node does not automatically remove the nil attribute. Then if you ever clear the value, you will need to set the nil attribute again (if desired) on that node.
Page 1 of 1 (4 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.