/// <summary> /// Get namespaces. /// </summary> /// <param name="document"></param> /// <returns></returns> public static IEnumerable <Tuple <string, string> > Namespaces(this XmlDocument document) { var manager = new XmlNamespaceManager(document.NameTable); var ns = manager.GetNamespacesInScope(XmlNamespaceScope.All); return(ns.Select(x => new Tuple <string, string>(x.Key, x.Value))); }
/// <summary> /// Gets all namespaces. /// </summary> public static IDictionary <string, string> GetAllNamespaces(XmlDocument xDoc) { var result = new XmlNamespaceManager(xDoc.NameTable); var xNav = xDoc.CreateNavigator(); while (xNav.MoveToFollowing(XPathNodeType.Element)) { var localNamespaces = xNav.GetNamespacesInScope(XmlNamespaceScope.Local); if (localNamespaces == null) { continue; } foreach (var localNamespace in localNamespaces) { var prefix = localNamespace.Key; if (string.IsNullOrEmpty(prefix)) { prefix = "DEFAULT"; } result.AddNamespace(prefix, localNamespace.Value); } } return(result.GetNamespacesInScope(XmlNamespaceScope.All)); }
private static void RetrieveMissingNamespaceElements( List <string> missingNamespaces, XmlDocument xmlDocument, XmlNamespaceManager manager) { try { IDictionary <string, string> namespacesInScope = manager.GetNamespacesInScope(XmlNamespaceScope.All); var fileNamespaces = namespacesInScope.Select(namespaceInScope => namespaceInScope.Key).ToList(); XmlNodeList nodes = xmlDocument.ChildNodes; if (nodes == null) { return; } var foundNamespaces = new List <string>(); // Retrieves nodes which use the namespace (i.e. declaring a control, <control:MyControl... />) RetrieveFileNodeNamespaces(nodes, foundNamespaces); // Retrieves Style nodes with TargetType which use the namespace (i.e. <Style TargetType="control:MyControl" />) RetrieveTargetTypeNamespaces(StyleTargetTypeXPath, "TargetType", foundNamespaces, xmlDocument, manager); // Retrieves nodes which use the namespace as an attribute (i.e. <TextBox control:MyControl.Text="Hello" />) RetrieveAttributeNamespaces(nodes, foundNamespaces); missingNamespaces.AddRange(foundNamespaces.Where(x => !fileNamespaces.Contains(x))); } catch (Exception ex) { EventLog.Error(ex); } }
public IEnumerable <XAttribute> GetAll() { return(_mgr .GetNamespacesInScope(XmlNamespaceScope.Local) .Select(_ns => String.IsNullOrEmpty(_ns.Key) ? new XAttribute("xmlns", _ns.Value) : new XAttribute(XNamespace.Xmlns + _ns.Key, _ns.Value))); }
public static string ToNSMethodFormat(this XmlNamespaceManager manager) { var namespaces = new List <string>(); foreach (var ns in manager.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml)) { namespaces.Add($"xmlns:{ns.Key}='{ns.Value}'"); } return(string.Join(" ", namespaces)); }
private static XmlSerializerNamespaces Convert(this XmlNamespaceManager xmlNamespaceManager) { XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces(); foreach (var ns in xmlNamespaceManager.GetNamespacesInScope(XmlNamespaceScope.Local)) { xmlSerializerNamespaces.Add(ns.Key, ns.Value); } return(xmlSerializerNamespaces); }
public void TheNameSpaces() { Console.WriteLine("The namespace reader is called :"); var result = manager.GetNamespacesInScope(XmlNamespaceScope.All); Console.WriteLine($"Amount of namespaces: {result.Count}"); foreach (KeyValuePair <string, string> entry in result) { Console.WriteLine($"Entry Key: {entry.Key} and value: {entry.Value}"); } }
public static string GetRegisteredNamespaces(this XmlNamespaceManager manager) { var namespacesInScope = manager.GetNamespacesInScope(XmlNamespaceScope.Local); if (namespacesInScope == null) { return(null); } var registeredNamespaces = namespacesInScope .Select(x => string.Format("[{0}: '{1}']", x.Key, x.Value)).ToArray(); return(string.Join(", ", registeredNamespaces)); }
public void SetNamespaces(XmlNamespaceManager namespaceManager) { if (namespaceManager == null) { throw new ArgumentNullException(nameof(namespaceManager)); } this.xmlns = new XmlSerializerNamespaces(); var ns = namespaceManager.GetNamespacesInScope(XmlNamespaceScope.All); foreach (var prefix in ns.Keys) { this.xmlns.Add(prefix, ns[prefix]); } }
/// <inheritdoc /> public string Serialize(Type type, object data, XmlNamespaceManager xmlNamespaceManager, bool setDefaultNamespace = true) { XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces(); IDictionary <string, string> namespaces = xmlNamespaceManager?.GetNamespacesInScope(XmlNamespaceScope.Local); foreach (string prefix in namespaces?.Keys) { xmlSerializerNamespaces.Add(prefix, namespaces[prefix]); } using (var stringWriter = new StringWriter()) { using (var writer = XmlWriter.Create(stringWriter, _xmlWriterSettings)) { var serializer = GetSerializer(type, setDefaultNamespace ? xmlNamespaceManager?.DefaultNamespace : null); serializer.Serialize(writer, data, xmlSerializerNamespaces); return(stringWriter.ToString()); } } }
public string GetValue(XElement element, XmlNamespaceManager nsm) { XPathContext context = new XPathContext((NameTable)nsm.NameTable); XPathNavigator navigator = element.CreateNavigator(); object result = null; foreach (var ns in nsm.GetNamespacesInScope(XmlNamespaceScope.All)) { context.AddNamespace(ns.Key, ns.Value); } context.Arguments.AddParam(XPathContext.ParameterNames.CurrentNode, string.Empty, navigator.Select(".")); result = navigator.Evaluate(this.RawValue, context); if (result is string) { return((string)result); } else if (result is XPathNodeIterator) { var iterator = ((XPathNodeIterator)result); var current = (XPathNavigator)((IEnumerable)iterator).Cast <object>().First(); return(current.Value); } else if (result is XAttribute) { return(((XAttribute)result).Value); } else if (result is XElement) { return(((XElement)result).Value); } return(string.Empty); }
private void ProcessXsiAttributes(out XmlQualifiedName xsiType, out string xsiNil) { string[] xsiSchemaLocation = null; string xsiNoNamespaceSchemaLocation = null; xsiType = XmlQualifiedName.Empty; xsiNil = null; if (reader.Depth == 0) { //Load schema for empty namespace LoadSchema(string.Empty, null); //Should load schemas for namespaces already added to nsManager foreach (string ns in nsManager.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml).Values) { LoadSchema(ns, null); } } if (reader.MoveToFirstAttribute()) { do { string objectNs = reader.NamespaceURI; string objectName = reader.LocalName; if (Ref.Equal(objectNs, NsXmlNs)) { LoadSchema(reader.Value, null); if (bManageNamespaces) { nsManager.AddNamespace(reader.Prefix.Length == 0 ? string.Empty : reader.LocalName, reader.Value); } } else if (Ref.Equal(objectNs, NsXsi)) { if (Ref.Equal(objectName, XsiSchemaLocation)) { xsiSchemaLocation = (string[])dtStringArray.ParseValue(reader.Value, NameTable, nsManager); } else if (Ref.Equal(objectName, XsiNoNamespaceSchemaLocation)) { xsiNoNamespaceSchemaLocation = reader.Value; } else if (Ref.Equal(objectName, XsiType)) { xsiType = (XmlQualifiedName)dtQName.ParseValue(reader.Value, NameTable, nsManager); } else if (Ref.Equal(objectName, XsiNil)) { xsiNil = reader.Value; } } } while(reader.MoveToNextAttribute()); reader.MoveToElement(); } if (xsiNoNamespaceSchemaLocation != null) { LoadSchema(string.Empty, xsiNoNamespaceSchemaLocation); } if (xsiSchemaLocation != null) { for (int i = 0; i < xsiSchemaLocation.Length - 1; i += 2) { LoadSchema((string)xsiSchemaLocation[i], (string)xsiSchemaLocation[i + 1]); } } }
public ChoXmlNamespaceManager(XmlNamespaceManager nsMgr) { NSDict = nsMgr.GetNamespacesInScope(XmlNamespaceScope.All); }
protected virtual IDictionary <string, string> GetNamespacesFromManager(XmlNamespaceManager manager) { return(manager.GetNamespacesInScope(XmlNamespaceScope.All)); }
private List <IRow> Xml2Rows(string xml) { try { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); IDictionary <string, string> dic = ns.GetNamespacesInScope(XmlNamespaceScope.All); string nsKey = String.Empty; if (xml.Contains("http://www.gviewgis.com/wms")) { ns.AddNamespace("gview_wms", "http://www.gviewgis.com/wms"); nsKey = "gview_wms"; } List <IRow> rows = new List <IRow>(); if (!String.IsNullOrEmpty(nsKey)) { foreach (XmlNode feature in doc.SelectNodes("gview_wms:FeatureInfoResponse/gview_wms:FeatureInfoCollection/gview_wms:FeatureInfo", ns)) { Row row = new Row(); foreach (XmlNode field in feature.SelectNodes("gview_wms:Field", ns)) { XmlNode fieldName = field.SelectSingleNode("gview_wms:FieldName", ns); XmlNode fieldValue = field.SelectSingleNode("gview_wms:FieldValue", ns); if (fieldName != null && fieldValue != null) { row.Fields.Add(new FieldValue( fieldName.InnerText, fieldValue.InnerText)); } } rows.Add(row); } } else { foreach (XmlNode feature in doc.SelectNodes("//FEATURE")) { Row row = new Row(); foreach (XmlNode field in feature.SelectNodes("FIELDS/FIELD")) { if (field.Attributes["name"] == null || field.Attributes["value"] == null) { continue; } row.Fields.Add(new FieldValue( field.Attributes["name"].Value, field.Attributes["value"].Value)); } rows.Add(row); } } return(rows); } catch { return(null); } }
public void GetNamespacesInScope() { XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); Assert.AreEqual(0, nsmgr.GetNamespacesInScope(l).Count, "#1"); Assert.AreEqual(0, nsmgr.GetNamespacesInScope(x).Count, "#2"); Assert.AreEqual(1, nsmgr.GetNamespacesInScope(a).Count, "#3"); nsmgr.AddNamespace("foo", "urn:foo"); Assert.AreEqual(1, nsmgr.GetNamespacesInScope(l).Count, "#4"); Assert.AreEqual(1, nsmgr.GetNamespacesInScope(x).Count, "#5"); Assert.AreEqual(2, nsmgr.GetNamespacesInScope(a).Count, "#6"); // default namespace nsmgr.AddNamespace("", "urn:empty"); Assert.AreEqual(2, nsmgr.GetNamespacesInScope(l).Count, "#7"); Assert.AreEqual(2, nsmgr.GetNamespacesInScope(x).Count, "#8"); Assert.AreEqual(3, nsmgr.GetNamespacesInScope(a).Count, "#9"); // PushScope nsmgr.AddNamespace("foo", "urn:foo"); nsmgr.PushScope(); Assert.AreEqual(0, nsmgr.GetNamespacesInScope(l).Count, "#10"); Assert.AreEqual(2, nsmgr.GetNamespacesInScope(x).Count, "#11"); Assert.AreEqual(3, nsmgr.GetNamespacesInScope(a).Count, "#12"); // PopScope nsmgr.PopScope(); Assert.AreEqual(2, nsmgr.GetNamespacesInScope(l).Count, "#13"); Assert.AreEqual(2, nsmgr.GetNamespacesInScope(x).Count, "#14"); Assert.AreEqual(3, nsmgr.GetNamespacesInScope(a).Count, "#15"); nsmgr.AddNamespace("", ""); // MS bug - it should return 1 for .Local but it returns 2 instead. //Assert.AreEqual (1, nsmgr.GetNamespacesInScope (l).Count, "#16"); Assert.AreEqual(1, nsmgr.GetNamespacesInScope(x).Count, "#17"); Assert.AreEqual(2, nsmgr.GetNamespacesInScope(a).Count, "#18"); }
public IDictionary <string, string> GetNamespacesInScope(XmlNamespaceScope scope) { return(_manager.GetNamespacesInScope(scope)); }