1 //------------------------------------------------------------------------------
2 // <copyright file="SchemaNamespaceManager.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7 
8 namespace System.Xml.Schema {
9     using System;
10     using System.Diagnostics;
11     using System.Collections;
12 
13     internal class SchemaNamespaceManager : XmlNamespaceManager {
14         XmlSchemaObject node;
15 
SchemaNamespaceManager(XmlSchemaObject node)16         public SchemaNamespaceManager(XmlSchemaObject node) {
17             this.node = node;
18         }
19 
LookupNamespace(string prefix)20         public override string LookupNamespace(string prefix) {
21             if (prefix == "xml") { //Special case for the XML namespace
22                 return XmlReservedNs.NsXml;
23             }
24             Hashtable namespaces;
25             for (XmlSchemaObject current = node; current != null; current = current.Parent) {
26                 namespaces = current.Namespaces.Namespaces;
27                 if (namespaces != null && namespaces.Count > 0) {
28                     object uri = namespaces[prefix];
29                     if (uri != null)
30                         return (string)uri;
31                 }
32             }
33             return prefix.Length == 0 ? string.Empty : null;
34         }
35 
LookupPrefix(string ns)36         public override string LookupPrefix(string ns) {
37             if (ns == XmlReservedNs.NsXml) { //Special case for the XML namespace
38                 return "xml";
39             }
40             Hashtable namespaces;
41             for (XmlSchemaObject current = node; current != null; current = current.Parent) {
42                 namespaces = current.Namespaces.Namespaces;
43                 if (namespaces != null && namespaces.Count > 0) {
44                     foreach(DictionaryEntry entry in namespaces) {
45                         if (entry.Value.Equals(ns)) {
46                             return (string)entry.Key;
47                         }
48                     }
49                 }
50             }
51             return null;
52         }
53 
54   }; //SchemaNamespaceManager
55 }
56