How Do I Add Root Level Attributes?

Problem

It is sometimes necessary to add additional attributes to the root element, a good example is the schemaLocation attribute which indicates where the schema that validates the document can be found.

Resolution

The code below demonstrates how to insert the schemaLocation item into the root element

<?xml version="1.0"?> <purchaseReport xmlns="http://www.example.com/Report"                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                 xsi:schemaLocation="http://www.example.com/Report                 http://www.example.com/Report.xsd">         ... </purchaseReport>

Sample Code

  C++

CLtXmlLib16::CXmlSerializationContext::GetDefaultContext() .GetAdditionalRootAttributes()->Add( _T("schemaLocation"), _T("http://www.example.com/Report http://www.example.com/Report.xsd"), _T("http://www.w3.org/2001/XMLSchema-instance"));

  C#

using LiquidTechnologies.Runtime.Net40; ... XmlSerializationContext.Default.AdditionalRootAttributes.Add( "schemaLocation", "http://www.example.com/Report http://www.example.com/Report.xsd", "http://www.w3.org/2001/XMLSchema-instance");

  Java

import com.liquid_technologies.ltxmllib16; ... XmlSerializationContext.Default.getAdditionalRootAttributes.add( "schemaLocation", "http://www.example.com/Report http://www.example.com/Report.xsd", "http://www.w3.org/2001/XMLSchema-instance");

  Visual Basic

LtXmlComLib16.DefaultXmlSerializationContext.AdditionalRootAttributes.AddNewEx _ "schemaLocation", _ "http://www.w3.org/2001/XMLSchema-instance", _ "http://www.example.com/Report http://www.example.com/Report.xsd"

 

Notes

Descrption Value
Article Created 7/2/2006
Versions Liquid XML 2005 (4.1.0) and greater
Also See Setting A Default Namespace
  Working In a multithreaded environment
 

Working With Multiple Schemas

  Hand Coded Blocks