/
XmlSerializerNamespaces.cs
150 lines (124 loc) · 4.38 KB
/
XmlSerializerNamespaces.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
namespace System.Xml.Serialization
{
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public class XmlSerializerNamespaces
{
private Dictionary<string, XmlQualifiedName>? _namespaces;
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public XmlSerializerNamespaces()
{
}
/// <internalonly/>
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public XmlSerializerNamespaces(XmlSerializerNamespaces namespaces)
{
_namespaces = new Dictionary<string, XmlQualifiedName>(namespaces.NamespacesInternal);
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public XmlSerializerNamespaces(XmlQualifiedName[] namespaces)
{
_namespaces = new Dictionary<string, XmlQualifiedName>(namespaces.Length);
foreach (var qname in namespaces)
_namespaces.Add(qname.Name, qname);
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
internal XmlSerializerNamespaces(IList<XmlQualifiedName> namespaces)
{
_namespaces = new Dictionary<string, XmlQualifiedName>(namespaces.Count);
foreach (var qname in namespaces)
_namespaces.Add(qname.Name, qname);
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void Add(string prefix, string? ns)
{
// parameter value check
if (prefix != null && prefix.Length > 0)
XmlConvert.VerifyNCName(prefix);
if (ns != null && ns.Length > 0)
XmlConvert.ToUri(ns);
AddInternal(prefix!, ns);
}
internal void AddInternal(string prefix, string? ns)
{
NamespacesInternal[prefix] = new XmlQualifiedName(prefix, ns);
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public XmlQualifiedName[] ToArray()
{
if (_namespaces == null || _namespaces.Count == 0)
return Array.Empty<XmlQualifiedName>();
XmlQualifiedName[] array = new XmlQualifiedName[_namespaces.Count];
_namespaces.Values.CopyTo(array, 0);
return array;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public int Count
{
get { return (_namespaces == null) ? 0 : _namespaces.Count; }
}
internal Dictionary<string, XmlQualifiedName>.ValueCollection Namespaces => NamespacesInternal.Values;
private Dictionary<string, XmlQualifiedName> NamespacesInternal => _namespaces ??= new Dictionary<string, XmlQualifiedName>();
internal ArrayList? NamespaceList
{
get
{
if (_namespaces == null || _namespaces.Count == 0)
return null;
return new ArrayList(_namespaces.Values);
}
}
internal bool TryLookupPrefix(string? ns, out string? prefix)
{
prefix = null;
if (_namespaces == null || _namespaces.Count == 0 || string.IsNullOrEmpty(ns))
return false;
foreach (var nsPair in _namespaces)
{
if (!string.IsNullOrEmpty(nsPair.Key) && nsPair.Value.Namespace == ns)
{
prefix = nsPair.Key;
return true;
}
}
return false;
}
internal bool TryLookupNamespace(string? prefix, out string? ns)
{
ns = null;
if (_namespaces == null || _namespaces.Count == 0 || string.IsNullOrEmpty(prefix))
return false;
if (_namespaces.TryGetValue(prefix, out XmlQualifiedName? qName))
{
ns = qName.Namespace;
return true;
}
return false;
}
}
}