ILOG Rules for .NET User Guides > Rule Studio > Executing Rules > Using XML Data

You can generate .NET classes based on XML data and include them in a business object model. This enables you to write business rules that can be executed against data stored as XML.

In order to run rules against XML data, ILOG Rules for .NET uses a mapping between XML Schema and CLR type systems.

To generate the .NET classes you use the XmlSchemaCodeGenerator tool included with ILOG Rule Studio for .NET. Classes can be automatically generated from an XML Schema by setting XmlSchemaCodeGenerator as the custom tool.

The mechanism used by the XmlSchemaCodeGenerator tool is similar to the one provided by the XML Schema Definition tool in the .NET Framework. The advantage of using the XmlSchemaCodeGenerator tool is that you can write an XML Schema within Visual Studio .NET, and incrementally update your model as the Schema evolves without leaving the development environment.

Note
You must not modify the generated source code as your changes will be lost whenever the code is generated due to a change in the XML Schema. If you want to add methods to the generated classes, you must use the extender mechanism provided by Rule Studio. The extender methods will be managed in a separate source file and will not be altered when you regenerate the classes.

To specify the custom tool for a schema in a solution
  1. Create an XML Schema (*.xsd) file that defines the data.
  2. Add the XML Schema file to a class library project that is referenced by the business object model.
  3. In the Solution Explorer, right-click the item corresponding to your schema and, on the shortcut menu, click Properties.
  4. Set Custom Tool to: XmlSchemaCodeGenerator. The XmlSchemaCodeGenerator tool generates .NET classes every time you save the XML Schema.
  5. Once you have generated .NET classes for the XML Schema you can import them into a business object model using the Business Object Model Wizard, and write business rules about the generated classes.

The source code is automatically generated each time you save changes to the XML Schema.

To read an XML object and bind it to a variable
  1. Use the following code:
XmlSerializer serializer = new XmlSerializer(typeof(GeneratedClass)); 
XmlTextReader reader = null; 
GeneratedClass myObject = null; 
    try { 
        reader = new XmlTextReader(@"XMLObject.xml"); 
        myObject = (GeneratedClass)serializer.Deserialize(reader); 
        } 
    finally { 
        if (reader != null) 
        reader.Close(); 
    } 
To create an engine and execute your rules on the XML object
  1. Use the following code:
RuleEngine engine = new RuleEngine(); 
engine.Assert(myObject); 
engine.RuleSet = new RuleProjectName(); 
engine.Execute(); 
To serialize objects back to XML after they have been processed by the rule engine

The generated classes are designed to work with the XML Serialization classes of the .NET Framework. So, to instantiate and use the XML objects in an application that embeds the ILOG Rule Engine for .NET, you map the XML data stream to a set of objects, add these objects to the working memory, run the business rules, and, optionally, serialize the result objects back into an XML stream.

The following sample shows how to use the XmlSerializer class:

[C#]

// Construct an instance of XmlSerializer
// with the type of object being [de]serialized.
XmlSerializer serializer =
new XmlSerializer(typeof(MyClass));
// Create an XML reader
// (a generic stream can be used as well)
XmlReader reader =
new XmlTextReader(“input.xml”);
// Deserialize
MyClass myObject =
(MyClass) serializer.Deserialize(reader);
// Add myObject into the working memory
// (...)
// Create an XML writer
// (a generic stream can be used as well)
XmlWriter writer =
new XmlTextWriter(“output.xml”);
// Serialize the result.
serializer.Serialize(writer, myObject);
 

[Visual Basic]

` Construct an instance of XmlSerializer
` with the type of object being [de]serialized.
Dim serializer as XmlSerializer
serializer = new XmlSerializer(GetType(MyClass))
` Create an XML reader
` (a generic stream can be used as well)
Dim reader as XmlReader
reader =
new XmlTextReader("input.xml")
` Deserialize
Dim myObject as MyClass
myObject = CType(serializer.Deserialize(reader), MyClass)
` Add myObject into the working memory
` (...)
` Create an XML writer
` (a generic stream can be used as well)
Dim writer as XmlWriter
writer = new XmlTextWriter("output.xml")
` Serialize the result.
serializer.Serialize(writer, myObject)

See Also

Retrieving Data and Events From the Engine | Retrieving Data and Events From the Engine | Setting an Exception Handler on the Engine Execution | XML Binding Sample