Tuesday, August 10, 2010

Adding a node usine Microsoft.RuleEngine.XMLHelper

Adding a node without a namespace in BRE using Microsoft.RuleEngine.XMLHelper can be done quite easily with AddNode method.
For eg :
Step 1. XmlHelper.AddNode(AcCanonicalSchemas.AcCommonPCTypes.MsgStatus:/MsgStatus, . ,ExtendedStatus)
2. XmlHelper.AddNodeWithValue(AcCanonicalSchemas.AcCommonPCTypes.MsgStatus:/MsgStatus, ./ExtendedStatus[position()=last()], ExtendedStatusCd, Code added in this node through BRE)
Step 1 adds a node to ExtendedStatus under MsgStatus TypedXMLDocument.
Step 2 adds a node ExtendedStatusCd and its value under ExtendedStatus node.

This is fine untill the TypedXMLDocument MsgStatus is without any namespace. If MsgStatus has a namespace for eg http://AcCanonicalSchemas.PANDC then the above code with addnode will generate xml as

<MsgStatus xmlns="http://AcCanonicalSchemas.PANDC"><ExtendedStatus xmlns=""><ExtendedStatusCd>Code added in this node through BRE</ExtendedStatusCd></ExtendedStatus></MsgStatus>

Which cannot be validated. To add a node with the same namespace, use the Xpath as below in .
Step 1
XmlHelper.AddNode(AcCanonicalSchemas.AcCommonPCTypes.MsgStatus:/MsgStatus, . ,ExtendedStatus, http://AcCanonicalSchemas.PANDC)
2. XmlHelper.AddNodeWithValue(AcCanonicalSchemas.AcCommonPCTypes.MsgStatus:/MsgStatus, ./*[local-name()='ExtendedStatus' and namespace-uri()=http://AcCanonicalSchemas.PANDC'][position()=last()], ExtendedStatusCd, Code added in this node through BRE)

This will generate Xml as

<MsgStatus xmlns="http://AcCanonicalSchemas.PANDC"><extendedstatus><ExtendedStatusCd>Code added in this node through BRE</ExtendedStatusCd></ExtendedStatus></MsgStatus>


This will make sure all nodes are under the same namespace.

No comments: