How to manipulate InfoPath form's datasource

  • Thread starter Morten Wennevik
  • Start date
M

Morten Wennevik

Hi,

I'm trying to dynamically add items in a DropDownList. The DropDownList
uses a repeating field my:ListeLookup to fill the values and this field
has no values in it to begin with. I add values to the xml in an external
program using this code

Dim doc As New System.Xml.XmlDocument()
doc.LoadXml(XML43.XmlString)

Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("my",
"http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-06-16T08:03:28")

Dim node As XmlNode = doc.SelectSingleNode("my:nyKunde/my:ListeLookup",
nsmgr)

Dim newNode As XmlNode = doc.CreateNode(XmlNodeType.Element,
"my:NewName",
"http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-06-16T08:03:28")
newNode.InnerText = "Hello World"

node.AppendChild(newNode)

XML43.XmlString = doc.OuterXML

In a pane in the InfoPath form this xml is replaced with the existing
using replaceChild but I get this error

Element
'{http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-06-16T08:03:28}NewName'
is unexpected according to content element of parent element
'{http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-06-16T08:03:28}ListeLookup'

How can I get NewName to be expected?

- Morten
 
D

darko

Hi,

in order to add xmlnode, try to select first node from the initial xml, and
call the Clone method and assign tie return value to the new XmlNode
variable. With this action, you get new node, with the same format as the
initial nodes. Then, add values to these node, and try the metrod
insertBefore in order to add the node to the data source.

Darko Milevski, MCAD
 
M

Morten Wennevik

Hi Darko,

I'm afraid this method does not work for me.

using the code below I get an error on appendChild saying the new node
(child) is unexpected according to content model of parent node

this.XDOM = window.external.Window.XDocument.DOM;

this.XDOM.setProperty("SelectionNamespaces",
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-06-16T08:03:28"
xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"');

var parent = this.XDOM.selectSingleNode("//my:ListeLookup");

var child = parent.cloneNode(true);

parent.appendChild(child);


How do I get XDocument to accept the new node?
 
D

darko

Hi,

If I asume that youre xml is like this
<ListLookup>
<Value></Value>
<Value></Value>
....
</ListLookup>
youre code should be
this.XDOM = window.external.Window.XDocument.DOM;
var beforeNode = this.XDOM.selectSingleNode("//my:Value"); //selects the
first value node
var child = beforeNode .cloneNode(true);
this.XDOM.insertBefore(child,beforeNode);

you dont need to setProperty, becouse the cloneNode will make the new
addNode child to be same as the selected one.

Darko Milevski, MCAD
 
M

Morten Wennevik

Hi Darko,

The Xml structure is
<my:nyKunde><my:ListeLookup></myListeLookup></my:nyKunde> and the
DropDownList is set to use the repeating field ListeLookup for filling the
list.

However, since I couldn't add another ListeLookup field in the designed I
assumed the list was filled with sub nodes to ListeLookup, which was not
true and any child node of ListeLookup was unexpected according to content
model of parent...

Adding the new node to nyKunde instead of ListeLookup solved the problem..
insertBefore would probably work as well as the node would then be added
to the parent of ListeLookup (or parent of Value in your case).

I do, however, need setProperty as otherwise selectSingleNode failes as
'my' is not a defined namespace. The code runs in a custom task pane.

Thanks for your help.

Morten
 

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