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.

Tuesday, August 3, 2010

XSD.exe not able to generate C# classes when the schema uses imports

XSD.Exe is unable to generate C# classes when an import function is used to use the existing types from another schema. It throws a warning like
Schema could not be validated. Class generation may fail or may produce incorrect results.
Solution : Copy the imported schema(Schema with reuse types) to the same folder as the schema which is using its types and also don't use the reference when importing to get to the reusable types instead use the schema which is copied to the local folder and then execute the following command.

Xsd.exe NewSchema.xsd SchemaWithReuseTypes.xsd /c this should generate the corresponding c# class.