2
// MonoDevelop XML Editor
4
// Copyright (C) 2006-2007 Matthew Ward
6
// Permission is hereby granted, free of charge, to any person obtaining
7
// a copy of this software and associated documentation files (the
8
// "Software"), to deal in the Software without restriction, including
9
// without limitation the rights to use, copy, modify, merge, publish,
10
// distribute, sublicense, and/or sell copies of the Software, and to
11
// permit persons to whom the Software is furnished to do so, subject to
12
// the following conditions:
14
// The above copyright notice and this permission notice shall be
15
// included in all copies or substantial portions of the Software.
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
using MonoDevelop.Core;
27
using System.Collections.Generic;
30
namespace MonoDevelop.XmlEditor
32
public class XPathNamespaceList : ICustomXmlSerializer
34
const string xpathNamespaceListElementName = "XPathNamespaceList";
35
const string xpathNamespaceElementName = "Namespace";
37
List<XmlNamespace> namespaces = new List<XmlNamespace>();
39
public XPathNamespaceList()
44
/// Adds a single namespace to the list.
46
public void Add(string prefix, string uri)
48
namespaces.Add(new XmlNamespace(prefix, uri));
52
/// Gets the namespaces.
54
public XmlNamespace[] GetNamespaces()
56
return namespaces.ToArray();
60
/// Creates an XPathNamespaceList from the saved xml.
62
public ICustomXmlSerializer ReadFrom(XmlReader reader)
64
XPathNamespaceList list = new XPathNamespaceList();
65
bool finished = false;
66
while (reader.Read() && !finished) {
67
switch (reader.NodeType) {
68
case XmlNodeType.Element:
69
if (reader.Name == xpathNamespaceElementName) {
70
XmlNamespace ns = XmlNamespace.FromString(reader.ReadElementString());
71
list.Add(ns.Prefix, ns.Uri);
74
case XmlNodeType.EndElement:
75
if (reader.Name != xpathNamespaceElementName) {
85
/// Creates an xml element from an XPathNamespaceList.
87
public void WriteTo(XmlWriter writer)
89
writer.WriteStartElement(xpathNamespaceListElementName);
90
foreach (XmlNamespace ns in namespaces) {
91
writer.WriteElementString(xpathNamespaceElementName, ns.ToString());
93
writer.WriteEndElement();