Setting non-text values using selectSingleNode - InfoPath Dev
in

InfoPath Dev

Having trouble finding a blog or post that answers your question? Check out our Custom Search Page

Setting non-text values using selectSingleNode

Last post 09-09-2012 05:49 PM by sudlev. 19 replies.
Page 1 of 2 (20 items) 1 2 Next >
Sort Posts: Previous Next
  • 04-21-2008 02:27 PM

    • KoopLaFez
    • Not Ranked
      Male
    • Joined on 01-22-2008
    • San Diego, CA
    • Posts 20

    Setting non-text values using selectSingleNode

    I'm trying to set the values of fields on my XML form using javascript.   I have no problem setting text values using the selectSingleNode method:

     XDocument.DOM.selectSingleNode("/my:myFields/my:strField1").text = "hello world";

     However, if I wan to set the value of a non-text field usinig similar code:

     XDocument.DOM.selectSingleNode("/my:myFields/my:dtField1") = "5/5/2008";

     Or even the hypothetical:

      XDocument.DOM.selectSingleNode("/my:myFields/my:dtField1").date = "5/5/2008";

     I get the error, "Object does not support this property or method." 

    Now, I realize that I'm representing my date as a string here, which probably isn't helping things, but this brings up another issue:  How do represent dates in javascript?  In VBA, I would use the number sign to surround the date (e.g. dtToday = #4/21/2008#), but in javascript I get an invalid character error.

    Since I was working with a string anyway, I thought I'd go back to my original statement to see if it would work with the date:

     XDocument.DOM.selectSingleNode("/my:myFields/my:dtField1").text = "5/5/2008";

    Which gave me the error, "#PCData in nil content."  I went into the schema and removed the nillable attribute on the date field, which allowed me to force my string into the date field, but of course it created a validation error because I was forcing a string into a date field.  At this point it occurred to me that I was probably making things harder than they had to be, so I thought I'd post it here.

    So, basically, my question is this:  If I have a date field on an XML form, how do I assign it the value 5/5/2008 (or any specific date value) using javascript?

  • 04-21-2008 04:55 PM In reply to

    Re: Setting non-text values using selectSingleNode

    First, nodes have two useful properties (well really just the one): .text and .nodeTypedValue. Basically the first is useful and the second does essentially the same thing. So trying to set .date (which doesn't even exist) won't help you.

    Second, XML expects date in the sortable format, which is "yyyy-MM-dd", so setting a field with a date datatype, you need to set it as "2008-05-05".

    If your date field is set as not required, then it will have the attribute xsi:nil="true" on it. This attribute says that the field CAN be blank... but it also says that it MUST be blank. Non String datatypes must use this attribute to identify that they can be blank. In order to set your date value, you must first remove this attribute from the node, or else your attempt to set a value will conflict with the "MUST be blank" part. When you clear the value of the date field, it will now be a required field unless you add the xsi:nil="true" attribute back on to the node.

    Hope this helps!

    Visit http://www.braintrove.com
  • 04-22-2008 08:09 AM In reply to

    • KoopLaFez
    • Not Ranked
      Male
    • Joined on 01-22-2008
    • San Diego, CA
    • Posts 20

    Re: Setting non-text values using selectSingleNode

     Thanks Greg, it worked great.  One more thing though:  What format would I use for a date/time value?  I tried "2008-04-22 08:01:00" but that gave me validation error.

    Also, out of curiosity, what's the logic behind th nillable attribute?  I can see the benifit of having  field that can be blank, but what's the purpose of creating a field tha must be blank, and of what value is that field if must be blank?  And does this mean that any non-text field that I want to edit programatically must also be a required field?  I'm new to XML so I'm still trying to wrap my head around some of these concepts.

     Thanks again for all your help.

  • 04-23-2008 09:26 AM In reply to

    Re: Setting non-text values using selectSingleNode

    You are close... change the space character to a "T" character: "2008-04-22T08:01:00".

    Please keep in mind that these are not InfoPath-specific standards, they are general XML standards. You can ask here, but you can also find all this information in XML documentation and specs.

    Visit http://www.braintrove.com
  • 07-02-2009 10:13 AM In reply to

    Re: Setting non-text values using selectSingleNode

    Thank goodness I found this.  I never would have known about the forced date format when programmatically setting a date value on an infopath form and how to get rid of the nil error I kept recieving! 

    Thank you!

  • 04-01-2011 12:49 PM In reply to

    • pamam
    • Not Ranked
    • Joined on 03-28-2011
    • Posts 6

    Re: Setting non-text values using selectSingleNode

    How do you remove an attribute programmatically

    Filed under:
  • 04-19-2011 03:40 PM In reply to

    Re: Setting non-text values using selectSingleNode

     To remove the nil attribute (required before setting a value to a field with the nil attribute), you select the attribute the same way you would select any node, and then delete it. Then you can set the value.

    Visit http://www.braintrove.com
  • 04-20-2011 08:05 AM In reply to

    • pamam
    • Not Ranked
    • Joined on 03-28-2011
    • Posts 6

    Re: Setting non-text values using selectSingleNode

    Thanks for the help,

    Mel

  • 09-03-2012 10:27 PM In reply to

    Re: Setting non-text values using selectSingleNode

    Hi Greg,

    Can you assist me with this? I am unable to get my code to work (and I am not a programmer so I've really struggled with this).

    What I want to do is set an int field to null or equivalent. I tried using ValueNode.SetValue("") but that gived me invalid data.

    I really appreciate any assistance you can offer.

    Danny

  • 09-04-2012 04:26 AM In reply to

    Re: Setting non-text values using selectSingleNode

    The problem here is most likely the presence of an xsi:nil attribute.  Search this forum for xsi:nil, and you should find plenty of help.
    Jimmy Rishe / Software Developer / InfoPath MVP
    Qdabra Software
  • 09-04-2012 09:19 PM In reply to

    Re: Setting non-text values using selectSingleNode

    You were correct, in order to clear the value of the field I needed to add the xsl:nil attribute to the element. As you described in the how to add xsi:nil = "true" thread. I've pasted my code below for reference.
    _____________

    valueNode.SetValue(string.Empty);

    if (!valueNode.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))

    valueNode.CreateAttribute("xsi", "nil", System.Xml.Schema.XmlSchema.InstanceNamespace, "true");//required attribute for non-string values
    ________________________________

    Thanks for your help!

  • 09-05-2012 10:15 PM In reply to

    Re: Setting non-text values using selectSingleNode

    Hi again guys,

    My code (above) was working beautifully in preview mode in the designer, but now that it's been published it is failing again. Is there a reason this would not work in a browser form?

    Sorry to be a pest.

    Danny

  • 09-06-2012 07:06 AM In reply to

    Re: Setting non-text values using selectSingleNode

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 09-06-2012 08:06 AM In reply to

    Re: Setting non-text values using selectSingleNode

    You aren't a pest.

    It turns out this works differently in the browser - I had an email conversation with the InfoPath team at MS about this a couple of years ago, and was told:

                 /* ...there are differences in the way MSXML (InfoPath client XML library) and System.Xml (IPFS XML library)
                 * are validating the XML against the schema. After executing the SetValue() method, System.Xml
                 * recognizes the empty text node as a child of the “my:file” node. As such, a validation error
                 * occurs when you attempt to reapply the ‘nil’ attribute.*/

    So, I did this instead:

                // Because of this, modifying from 'SetValue' to 'DeleteSelf'. MS dev states the following works in client and browser.

                // For details on the below 2 lines, refer to:
                // http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.deleteself.aspx
                fileNode.MoveToFirstChild(); // This moves to the text value of the node.
                fileNode.DeleteSelf(); // This deletes the text value. On delete, node reverts to the parent.
                try
                {
                    // Not sure what data type is, so if this fails, it is probably string, so ignore.
                    fileNode.CreateAttribute("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
                }

    Hilary Stoupa
    Qdabra® Software/ InfoPathDev.com
    The InfoPath Experts – Streamline data gathering to turn process into knowledge.™

  • 09-06-2012 05:36 PM In reply to

    Re: Setting non-text values using selectSingleNode

    This works beautifully!

    I can't thank you enough Hilary, you've been a MASSIVE help! 

    Danny

Page 1 of 2 (20 items) 1 2 Next >
Copyright © 2003-2012 Qdabra Software. All rights reserved.
View our Terms of Use.