Problem selecting nodes

P

pearsond

Hi,

I am using the following code to open a new form from within an InfoPath
form and set a value of a field. However, I receive the following error:
System.Runtime.InteropServices.COMException
Reference to undeclared namespace prefix: 'my'.

Here is my code:
XDocumentsCollection docCollection = thisApplication.XDocuments;
XDocument newForm = docCollection.NewFromSolution(@"C:\Documents and
Settings\PearsonD\My Documents\MultiForm\Supporting.xsn");
newForm.DOM.selectSingleNode("/my:myFields/my:field1").text = "hello";

Here is xml from the form I'm trying to populate:
<?xml version="1.0" encoding="UTF-8"?>
<?mso-infoPathSolution solutionVersion="1.0.0.1" productVersion="11.0.6565"
PIVersion="1.0.0.0"
href="file:///C:\Documents%20and%20Settings\PearsonD\My%20Documents\Supporting.xsn"
name="urn:schemas-microsoft-com:eek:ffice:infopath:Supporting:-myXSD-2006-07-13T14-17-26" ?>
<?mso-application progid="InfoPath.Document"?>
<my:myFields
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-07-13T14:17:26" xml:lang="en-us">
<my:field1>Test Data</my:field1>
</my:myFields>

I greatly appreciate any assistance. Thank you.
 
S

S.Y.M. Wong-A-Ton

Try setting the namespace "my" by setting the "SelectionNamespaces" property
using "setProperty" on the DOM, before you start using XPath expressions to
find nodes. Search this newsgroup on "SelectionNamespaces" to find out how to
do this.
 
P

pearsond

Thank you for pointing me in the right direction. I was able to successfully
execute the xpath expressions using the following code:

// Access the Application and collection.
XDocumentsCollection docCollection = thisApplication.XDocuments;

// Create a new form from the passes URL.
XDocument newForm = docCollection.NewFromSolution(@"C:\Documents and
Settings\PearsonD\My Documents\MultiForm\Supporting.xsn");

// re-type
IXMLDOMDocument2 newFormDOM = (IXMLDOMDocument2) newForm.DOM;

// Set namespace property
newFormDOM.setProperty("SelectionNamespaces",
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-07-13T14:17:26\" xmlns:xd=\"http://schemas.microsoft.com/office/infopath/2003\"");

// Set field1.
newFormDOM.selectSingleNode("/my:myFields/my:field1").text = "hello";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top