Using the xmlDocumentComponent with schema
These steps show how to use the xmlDocumentComponent in StronglyTyped mode. Sample XML document and XSD Schema files are used.
Loading the OuterXML of an XML document
Use the solution created in the Using the xmlDocumentComponent topic. Rename the solution and assign to it this name: XML Document 1 Strongly Typed.
Modify the Windows form in the solution. This
form will be used to specify the name of the XML document to load,
display information from the document, update node information and
validate the XML. Complete the Windows form as follows:
Save the solution.
Copy the sample XML and XSD files to the solution
folder.
- Pubtest.xml
- Pubs1d.xsd
Depending where you save the Pubs1d.xsd file, you may need to modify
the Pubtest.xml file to make sure the path to the schema is correct.
Open the LoadDocument automation and select the xmlDocumentComponent from the Global tab.
Click the Edit Schema Provider link in the Properties grid. The Edit Schema Provider window is displayed.
Select XSD for the Select Schema Provider type.
Click Browse in the Specify Schema Provider Source field. Use the Open dialog to select the Pubs1d.xsd file and then click OK. The DocumentMode automatically changes to StronglyType and the SchemaProviderType displays as XSD.
Save the solution.
Adding Refresh and Clear functions
This solution contains functions for updating the document nodes. The Windows form contains a Refresh button for replacing the XML shown in the txtXML text box. The form also contains a New button for creating a new document based on the schema. Use these steps to add this functionality.
Open the LoadDocument automation.
Add the btnRefresh Click event to the automation.
Select the xmlDocumentComponent in the Global tab and add the OuterXML property for the component to the automation.
Add the txtXML text box Text property to the
automation and connect the components as shown below.
Save the solution.
Add the btnNew Click event to the automation.
Select the xmlDocumentComponent in the Global tab and add the ClearDocument method and OuterXML property to the automation.
Add the txtXML text box Text property to the
automation and connect the components as shown below.
Save and run the project.
Enter the path and file name for the XML document in the File Name text box and click the Load button. The XML document text should appear in the XML text box.
Click in the XML text box and enter some text to modify the XML text box contents.
Click Refresh. The original XML document text should display.
Click New. An empty XML document with the schema
structure should appear in the text box, as shown below.
Stop the project.
Adding an automation to validate the XML against the schema
Add an automation and assign this name to it: ValidateSchema. This automation will use the xmlDocumentComponent Validate method to validate the XML loaded against the Pubs1d.xsd schema.
Add the btnValidate Click method to the ValidateSchema automation.
Select the xmlDocumentComponent under the LoadDocument automation in Object Explorer. Add the Validate method for the component to the ValidateSchema automation.
Add a MessageDialog component and choose the (String Message): DialogResult overload. Set the message to Document Valid.
Select the xmlDocumentComponent and add the ValidateErrors property to the automation.
Add a ListLoop component to the automation
and connect the blocks as shown below.
Right-click the Item output data node of the
ListLoop and select Extract Proxy. An xmlDocumentValidationErrorProxy1
is added to the automation in the Local tab and is displayed on the
automation.
Add the Message property for the xmlDocumentValidationErrorProxy1 to the automation.
Add the Show (1 parameter) method for the MessageDialog
to the automation. Complete this part of the automation as shown below.
Save and run the project. Load the Pubtest.xml document and then click the Validate button. The Document Valid message is displayed. Next, load the Books.xml document which was used with the Using the xmlDocumentComponent topic. Click here to download the Books.xml file. Click the Validate button. A validation message is displayed.
Click OK and stop the solution.
Updating publisher information
Using the Publisher Operations tab of the Windows form, the solution is used to update Publisher nodes in the XML document. These methods are used:
CreatePublishers
AppendChildPublishers
PrependChildPublishers
InsertPublishers (2 parameters: Int32 index, Publishers element)
Follow these steps.
You will use the GetDocumentElement method to create an element instance for use with the Create, Append, Prepend, and Insert methods.
Add a new automation to the solution and assign this name to it: UpdatePublishers.
Add the btnPubXAppendChildXML Button Click event to the automation.
Select the xmlDocumentComponent listed under the LoadDocument automation in Object Explorer and add the GetDocumentElement method to the automation.
Right-click the Result output of the GetDocumentElement
method and select Extract Proxy. The dsPubsProxy, which contains an
instance of the element, is added to the automation. Connect these
components as shown below.
Add the xmlDocumentComponent CreatePublishers
method. Data input ports are included on the CreatePublishers method
for the Publishers XML node. Connect the output event from the dsPubsProxy
to the CreatePublishers method as shown below.
Add the Text properties for the following text
boxes to the UpdatePublishers automation:
- txtPubID
- txtPubName
- txtPubCity
- txtPubState
- txtPubCountry
Follow these steps.
Select the dsPubsProxy from the Local tab and add the AppendChildPublishers (1 parameter element) method. Note that since this is a strongly typed document, you must use the AppendChildPublishers method rather than the AppendChild method to append a child element.
Complete this part of the automation by making
the data and event connections shown here:
Click Edit > Select All.
With all of the blocks on the UpdatePublishers automation highlighted, click Automation > Group Objects. Assign this name to the group: Append Publishers. (See an example.)
Save and run the solution. Load the Pubtest.xml document and then select the Publisher Info tab on the Windows form.
Enter information in the PubID, Pub Name, City,
State, and Country text boxes and then click the Append Child (obj)
button. Click Refresh to display the updated XML in the XML text box.
The new Publisher node information should appear at the end of the
XML as shown below.
Stop the solution.
To Prepend a child node to the Publishers element, the PrependChildPublishers method for the dsPubsProxy object is used. The logic required for the PrependChildPublishers method is very similar to that used for the AppendChildPublishers .Follow these steps.
Select the Append Publishers group box, then click Edit > Copy.
Right-click an open part of the UpdatePublishers automation and click Paste. A duplicate copy of the Append Publishers group box is displayed on the automation. Change the name of this group box to Prepend Publishers.
Select the btnPubXPrependChild Click method.
Drag it to the automation and drop it on top of the btnPubXAppendChild
Click block. A dashed line will surround the block indicating you
are replacing the block with another object, as shown below.
When you release the mouse button, a confirmation message is displayed. Click Yes to replace the event.
Select the dsPubsProxy in the Local tab and then select the PrependChildPublishers (1 parameter element) method. Note that since this is a strongly typed document, you must use the PrependChildPublishers method rather than the PrependChild method to add a child element.
Drag and drop the PrependChildPublishers method on top of the dsPubsProxy1 AppendChildPublishers method in the Prepend Publishers group box. A dashed line will surround the block, indicating you are replacing the block with another object. When you release the mouse button, a confirmation message is displayed.
Click Yes to replace the method. This part
of the UpdatePublishers automation should look like the one shown
below.
Save and run the solution. Load the Pubtest.xml document and then select the Publisher Info tab on the Windows form.
Enter information in the PubID, Pub Name, City,
State, and Country text boxes and then click the Prepend Child (obj)
button. Click Refresh to display the updated XML in the XML text box.
The new Publisher node information should appear at the beginning
of the XML, as shown below.
Stop the solution.
To insert a Publishers node, you can use the InsertPublishers (2 parameters: Index, Publishers Element) method for the dsPubsProxy object. The logic required for the InsertPublishers method is very similar to that used for AppendChildPublishers and PrependChildPublishers. To complete this part of the automation, begin by selecting the Prepend Publishers group box.
With the Prepend Publishers group box highlighted, select the Copy option from the Edit menu.
Right-click an open part of the UpdatePublishers automation and select Paste. A duplicate copy of the Prepend Publishers group box is displayed in the automation. Since a new block for the InsertPublishers Index parameter will need to be added, ungroup the copy of the Prepend Publishers group box by highlighting the group box and selecting Ungroup Objects from the Automation menu.
Select the btnPubXInsert Click method. Drag
it to the automation and drop it on top of the btnPubXPrependChild
Click block. A dashed line will surround the block indicating you
are replacing the block with another object as shown below.
When you release the mouse button, a confirmation message is displayed. Click Yes to replace the event.
Delete the dsPubsProxy PrependChildPublishers method from the automation.
Select the dsPubsProxy in the Local tab and then add the InsertPublishers (2 parameters: Index, Publishers Element) method to the automation. Note that since this is a strongly typed document, you must use the InsertPublishers method rather than the Insert method to add an element.
Add the txtPublisherIndex text box, Text property to the automation.
Complete the automation as shown below.
Save and run the solution. Load the Pubtest.xml document and then select the Publisher Info tab on the Windows form.
Enter information in the PubID, Pub Name, City,
State, and Country text boxes. Enter a number in the Index text box
and then click the Insert (index, obj) button. Click Refresh to display
the updated XML in the XML text box. The new Publisher node information
should appear at the designated position in the XML. An example is
shown here:
Stop the solution.
Part V: Updating the New XML Document
By using the AppendChildPublishers and PrependChildPublishers methods, you can add nodes to a blank, new XML document. Follow these steps.
Run the solution.
Click New. XML is displayed in the XML text
box based on the Pubs1d.xsd schema.
Enter information in the PubID, Pub Name, City,
State, and Country text boxes, and then click the Append Child (obj)
button. Click Refresh to display the updated XML text in the XML text
box. The new Publisher node information should appear in the XML text
box. An example is shown here:
Related information
Using the xmlDocumentComponent
Privacy | Trademarks | Terms of Use | Feedback
Updated: 01 July 2024
© 2016 - 2024 Pegasystems Inc. Cambridge, MA All rights reserved.