private static XmlElement GetOrCreateChildElement(XmlElement parent, string name, string namespaceUri)
        {
            var namespaceManager = new XmlNamespaceManager(parent.OwnerDocument.NameTable);
            namespaceManager.AddNamespace(parent.GetPrefixOfNamespace(namespaceUri ?? parent.NamespaceURI), namespaceUri ?? parent.NamespaceURI);
            XmlNodeList nodeList = parent.SelectNodes(name, namespaceManager);

            if (nodeList.Count != 0)
                return (XmlElement) nodeList[0];

            return CreateChildElement(parent, name, namespaceUri);
        }
Beispiel #2
0
		static XmlElement VirualAttachTo(XmlElement e, XmlElement target) 
		{
			var prefix = target.GetPrefixOfNamespace(e.NamespaceURI);
			XmlElement newElement = e.OwnerDocument.CreateElement(prefix, e.LocalName, e.NamespaceURI);

			foreach (XmlAttribute a in target.Attributes) {
				if (a.Prefix == "xmlns" || a.Name == "xmlns") {
					newElement.Attributes.Append(a.Clone() as XmlAttribute);
				}
			}

			while (e.HasChildNodes) {
				newElement.AppendChild(e.FirstChild);
			}

			XmlAttributeCollection ac = e.Attributes;
			while (ac.Count > 0) {
				newElement.Attributes.Append(ac[0]);
			}
			
			return newElement;
		}
Beispiel #3
0
		string GetPrefixOfNamespace(string ns, XmlElement target)
		{
			var prefix = target.GetPrefixOfNamespace(ns);
			if (!string.IsNullOrEmpty(prefix))
				return prefix;
			var obj = this;
			while (obj != null)
			{
				prefix = obj.XmlElement.GetPrefixOfNamespace(ns);
				if (!string.IsNullOrEmpty(prefix))
					return prefix;
				obj = obj.ParentObject;
			}
			return null;
		}
 public static XmlAttribute GenerateNamespaceDeclaration(XmlElement context, XmlName name)
 {
     int count = 1;
     while (!string.IsNullOrEmpty(context.GetPrefixOfNamespace("uri:" + count))) {
         count++;
     }
     name.NamespaceUri = "uri:" + count;
     XmlAttribute xmlns = context.OwnerDocument.CreateAttribute("xmlns", name.Prefix, XmlHelpers.XmlnsUri);
     if (context.HasAttribute(xmlns.Name)) {
         // already have an attribute with this name! This is a tricky case where
         // user is deleting a namespace declaration.  We don't want to reinsert it
         // automatically in that case!
         return null;
     }
     xmlns.Value = name.NamespaceUri;
     return xmlns;
 }