Adding first row to repeating table

L

Laurie_Marsh

I have a main connection that looks like:

myFields
dataFields
d:COE_Employees
-- some stuff --
Comments
-- some attributes --
QF129
-- some attributes--

both Comments and QF129 are repeating. Here is the template.xml view:


<dfs:dataFields>
<d:COE_Employees EmployeeKey="" Name="">
-- other stuff --
<d:Comments CommentKey="" Engineer="" Date=""
Comment=""></d:Comments>
<d:QF129 Employee="" Date=""></d:QF129>
</d:COE_Employees>
</dfs:dataFields>

After the query is issued, there are no QF129 items in the DOM. During the
lifetime of the form, I want to add them programmatically but cannot use the
cloning method since there aren't any to clone from. Here is the code I have
to create the new node and add it to the DOM:

IXMLDOMNode rootNode = thisXDocument.DOM.selectSingleNode
("/dfs:myFields/dfs:dataFields/d:COE_Employees");
IXMLDOMNode QF129Node = thisXDocument.DOM.createNode(
DOMNodeType.NODE_ELEMENT, "d:QF129",
"http://schemas.microsoft.com/office/infopath/2003/ado/datafields");
IXMLDOMAttribute attr = thisXDocument.DOM.createAttribute
("Employee");
attr.value = rootNode.selectSingleNode("@EmployeeKey").text;
QF129Node.attributes.setNamedItem(attr);
attr = thisXDocument.DOM.createAttribute("Date");
attr.value = thisXDocument.DOM.selectSingleNode
("/dfs:myFields/my:WeekEnding").text;
QF129Node.attributes.setNamedItem(attr);
rootNode.appendChild(QF129Node);

The problem is that the call to appendChild fails with an error saying:

Element
'{http://schemas.microsoft.com/office/infopath/2003/ado/datafields}QF129' is
unexpected according to content model of parent element
'{http://schemas.microsoft.com/office/infopath/2003/ado/dataFields}COE_Employees'.

So, how do I add the first item for a repeating element programmatically?
 
S

S.Y.M. Wong-A-Ton

Most of the time when I've gotten this error, it wasn't a lie. The best way
to troubleshoot what you are missing when creating and appending your node is
to study the schema (XSD) for your form. The node that you're trying to
append does not agree with the schema.

Another way of appending a child node is to use CreateDOM() on thisXDocument
to create and IXMLDocument2 object. Then write the XML for your child node as
a string containing all its elements and attributes and use loadXML() to load
it into the document object you created. Then use appendChild() to add the
child node (= documentElement property on the document you created). Search
this newsgroup on "CreateDOM", "loadXML", and "documentElement"; you should
be able to find some code samples.
 
L

Laurie_Marsh

I couldn't figure out what I was doing that didn't match the schema. So, I
tried your second idea and that works great. Thanks for your help.
 
S

S.Y.M. Wong-A-Ton

Cool! Glad I could help.
---
S.Y.M. Wong-A-Ton


Laurie_Marsh said:
I couldn't figure out what I was doing that didn't match the schema. So, I
tried your second idea and that works great. Thanks for your help.
 

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