Altova MapForce 2025 Enterprise Edition

After you generate code from the example schema, a test C# application is created, along with several supporting Altova libraries. Recall that the example schema (Main.xsd) has multiple namespace declarations. Consequently, the generated code includes namespaces that correspond to namespace aliases (prefixes) from the schema, namely: Main.ord, Main.pur, Main.cmn, and Main.cust.

 

In general, in order to control XML namespaces and prefixes with the help of the schema wrapper libraries, you have the following methods at your disposal:

 

DeclareAllNamespacesFromSchema(). Call this method if you want to declare the same namespaces in your XML instance as in the schema. Otherwise, if you need different namespaces as in this example, then DeclareNamespace() should be used. The method DeclareAllNamespacesFromSchema() is not used in this example because we specifically want to create XML elements with prefixes that are slightly different from those declared in the schema.

DeclareNamespace(). Call this method to create or override the existing namespace prefix attribute on an element. The element must already be created using either the Append() or AppendWithPrefix() methods, as further illustrated below.

AppendWithPrefix(). Use this method to append an instance element with a specific prefix. To create the XML instance illustrated in this example, it was sufficient to call this method for the root element only. All other elements were appended using just Append(), and their prefixes were added automatically based on their namespaces, according to the rules above.

 

The code listing below shows you how to create an XML document with multiple namespace declarations and prefixed element names. Specifically, it generates a Purchase Order instance as illustrated in the Example: Purchase Order. Importantly, for illustrative purposes, some prefixes are overridden in the XML instance (that is, they are not exactly the same as the ones declared in the schema).

 

protected static void Example()
{
  // Create the XML document and append the root element
  pur.Main2 doc = pur.Main2.CreateDocument();
  pur.PurchaseType purchase = doc.Purchase.AppendWithPrefix("p");
 
  // Set schema location
  doc.SetSchemaLocation(@"Main.xsd");
 
  // Declare namespaces on root element
  purchase.DeclareNamespace("o", "http://NamespaceTest.com/OrderTypes");
  purchase.DeclareNamespace("c", "http://NamespaceTest.com/CustomerTypes");
  purchase.DeclareNamespace("cmn", "http://NamespaceTest.com/CommonTypes");
 
  // Append the OrderDetail element
  ord.OrderType order = purchase.OrderDetail.Append();
  ord.ItemType item = order.Item.Append();
  item.ProductName.Append().Value = "Lawnmower";
  item.Quantity.Append().Value = 1;
  item.UnitPrice.Append().Value = 148.42M;
 
  // Append the PaymentMethod element
  cmn.PaymentMethodTypeType paymentMethod = purchase.PaymentMethod.Append();
  paymentMethod.EnumerationValue = cmn.PaymentMethodTypeType.EnumValues.eVISA;
 
  // Append the CustomerDetails element
  cust.CustomerType customer = purchase.CustomerDetails.Append();
  customer.Name.Append().Value = "Alice Smith";
  cmn.AddressType deliveryAddress = customer.DeliveryAddress.Append();
  deliveryAddress.Line1.Append().Value = "123 Maple Street";
  deliveryAddress.Line2.Append().Value = "Mill Valley";
  cmn.AddressType billingAddress = customer.BillingAddress.Append();
  billingAddress.Line1.Append().Value = "8 Oak Avenue";
  billingAddress.Line2.Append().Value = "Old Town";
 
  // Save to file
  doc.SaveToFile("PurchaseOrder.xml", true);  
}

© 2018-2024 Altova GmbH