Microsoft KB Archive/317440

= BUG: Schema Object Model Does Not Recognize Valid Prefix Inside XPath Selector =

Article ID: 317440

Article Last Modified on 2/19/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q317440



The following .NET Framework Class Library namespaces are referenced in this article:

System.XML

System.XML.Schema



SYMPTOMS
When you use the Extensible Markup Language (XML) Schema Object Model (SOM) to create and to compile a schema from the very beginning, the SOM does not recognize a valid prefix inside an XML Path Language (XPath) selector. The prefix is added through the XmlSchema.Namespaces property.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior
 Start Visual Studio .NET. Create a new Windows application in Microsoft Visual C# .NET. Form1 is added to the project by default. Verify that your project contains a reference to the System.Data namespace. If it does not, add a reference to this namespace. Drag a Button control from the toolbox onto Form1. In the Properties window, change the Name property of the button to btnTest .  Use the using statement on the System.Xml and the System.Xml.Schema namespaces so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the &quot;General Declarations&quot; section of Form1: using System.Xml; using System.Xml.Schema;   Add the following code in the btnTest_Click event: XmlSchema schema = new XmlSchema; schema.Namespaces.Add(&quot;r&quot;, &quot;nsp&quot;); schema.Namespaces.Add(&quot;xs&quot;, &quot;http://www.w3.org/2001/XMLSchema&quot;); schema.ElementFormDefault = XmlSchemaForm.Qualified; schema.TargetNamespace = &quot;nsp&quot;;

XmlSchemaElement element = new XmlSchemaElement; element.Name = &quot;root&quot;;

XmlSchemaKey element_key = new XmlSchemaKey; element_key.Name = &quot;pNumKey&quot;;

XmlSchemaXPath element_key_selector = new XmlSchemaXPath; element_key_selector.XPath = &quot;r:part&quot;; element_key.Selector = element_key_selector;

XmlSchemaXPath field = new XmlSchemaXPath; field.XPath = &quot;@number&quot;; element_key.Fields.Add(field); element.Constraints.Add(element_key);

schema.Items.Add(element); schema.Compile(new ValidationEventHandler(ValidationCallBack)); schema.Write(Console.Out); </li>  Add the following code immediately after the btnTest_Click event: public void ValidationCallBack(object sender, ValidationEventArgs args) {   Console.WriteLine(args.Message); Console.WriteLine(args.Exception); }                   </li> Save your project. On the Debug menu, click Start to run your project. You receive the following error message in the output window:

The prefix 'r' in Xpath is unresolvable.

</li></ol>

<div class="references_section">