public void GetXmlSerializerNamespacesTest() { XmlSerializerNamespaces _newNamespaces = XmlHelper.GetXmlSerializerNamespaces(); Assert.AreEqual <int>(4, _newNamespaces.Count); Dictionary <string, XmlQualifiedName> _dic = _newNamespaces.ToArray().ToDictionary(x => x.Name); Assert.AreEqual <string>(@"http://opcfoundation.org/UA/2008/02/Types.xsd", _dic["ua"].Namespace); }
public XmlSerializerNamespaceResolver(XmlSerializerNamespaces serializerNamespaces) { serializerNamespaces.ToArray().ForEach(qns => { if (!qns.IsEmpty) { m_namespaces.Add(qns.Name, qns.Namespace); } }); }
// <Snippet1> private void PrintNamespacePairs(XmlSerializerNamespaces namespaces) { XmlQualifiedName[] qualifiedNames = namespaces.ToArray(); for (int i = 0; i < qualifiedNames.Length; i++) { Console.WriteLine ( qualifiedNames[i].Name + "\t" + qualifiedNames[i].Namespace ); } }
internal static string LoopupNamespace(XmlSerializerNamespaces namespaces, string prefix) { if (prefix != string.Empty) { return(string.Empty); } XmlQualifiedName[] XmlQualifiedNameList = namespaces.ToArray(); foreach (XmlQualifiedName XmlQualifiedName in XmlQualifiedNameList) { if (!XmlQualifiedName.IsEmpty && XmlQualifiedName.Name.Equals(prefix)) { return(XmlQualifiedName.Namespace); } } return(string.Empty); }
/// <summary> /// Merges namespaces. /// </summary> public static void Merge(this XmlSerializerNamespaces xmlns, XmlSerializerNamespaces other) { // Validate if (xmlns == null) { throw new ArgumentNullException("xmlns"); } if (other == null) { throw new ArgumentNullException("other"); } // Merge... foreach (var qname in other.ToArray()) { xmlns.Add(qname.Name, qname.Namespace); } }
private static void IsEquivalentTo(XmlSerializerNamespaces expected, XmlSerializerNamespaces actual) { Dictionary <string, XmlQualifiedName> actualNamespaces = actual.ToArray().ToDictionary(ns => $"{ns.Name}:{ns.Namespace}"); foreach (XmlQualifiedName expectedNs in expected.ToArray()) { if (!actualNamespaces.TryGetValue($"{expectedNs.Name}:{expectedNs.Namespace}", out XmlQualifiedName actualNs)) { throw new ContainsException(expectedNs.Namespace, actual); } actualNamespaces.Remove($"{actualNs.Name}:{actualNs.Namespace}"); } if (actualNamespaces.Count > 0 && (actualNamespaces.First().Key != "xsi:http://www.w3.org/2001/XMLSchema-instance" && actualNamespaces.First().Key != "xs:http://www.w3.org/2001/XMLSchema")) { throw new DoesNotContainException(expected, $"{actualNamespaces.First().Key.ToString()}: {actualNamespaces.First().Value.ToString()}"); } }
/// <summary> /// Initializes a new instance of the <see cref = "XmppSerializer" /> class. /// </summary> /// <param name = "elementName">Name of the element.</param> /// <param name = "schema">The schema.</param> /// <param name = "prefix">The prefix.</param> /// <param name = "defaultNamespace">The default namespace.</param> /// <param name = "serializerType">Type of the serializer.</param> private XmppSerializer(string elementName, string schema, string prefix, string defaultNamespace, Type serializerType) { this.elementName = elementName; this.serializerType = serializerType; this.schema = schema; this.prefix = prefix; this.defaultNamespace = defaultNamespace; serializer = new XmlSerializer(serializerType); nameTable = new NameTable(); nsMgr = new XmlNamespaceManager(nameTable); context = new XmlParserContext(nameTable, nsMgr, null, XmlSpace.None); namespaces = new XmlSerializerNamespaces(); namespaces.Add(prefix, defaultNamespace); foreach (XmlQualifiedName name in namespaces.ToArray()) { nsMgr.AddNamespace(name.Name, name.Namespace); } }
protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer) { writer.WriteStartDocument(); if (_message.Version.Envelope == EnvelopeVersion.Soap11) { writer.WriteStartElement("s", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/"); } else { writer.WriteStartElement("s", "Envelope", "http://www.w3.org/2003/05/soap-envelope"); } writer.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); foreach (var ns in _xmlNs?.ToArray()) { writer.WriteXmlnsAttribute(ns.Name, ns.Namespace); } }
void RebuildNamespacesAfterRead() { var namespaces = new XmlNamespaceManager(new NameTable()); foreach (var item in XmlSerializerNamespaces.ToArray()) { namespaces.AddNamespace(item.Name, item.Namespace); } var contextsWithMembers = Contexts.Where(c => c.Scenario != null); DimensionFromTypedMembers(namespaces, contextsWithMembers); DimensionFromExplicitMembers(namespaces, contextsWithMembers); GetFactNamespace(); UpdateContextNamespaces(namespaces, contextsWithMembers); Namespaces = namespaces; }
//***************************************************************** // Helpers //***************************************************************** /// <summary> /// Checks whether the prefix "xs" is defined as the Xsd uri. If it /// has been defined as something other then the Xsd uri and the /// xsd uri has not been mapped to a prefix then maps the Xsd uri to /// a unique prefix. /// </summary> /// <remarks> /// This is a work around specifically for the /// XmlSchema object. /// /// namespaces = The XmlSerializerNamespaces object that holds /// namespace mappings. /// serialWriter = The XmlSerializationWriter that will write out /// the serialized object. This object is used to /// create a unique prefix. /// </remarks> private void checkXsdNamespaceMapping(ref XmlSerializerNamespaces namespaces, XmlSerializationWriter serialWriter) { bool xsdRedefined = false, xsdUriPresent = false; const string XmlSchemaPrefix = "xs"; foreach (XmlQualifiedName qName in namespaces.ToArray()) { if (qName.Name == XmlSchemaPrefix && qName.Namespace != Soap.XsdUrl) { xsdRedefined = true; } if (qName.Namespace == Soap.XsdUrl) { xsdUriPresent = true; } } if (xsdRedefined && !xsdUriPresent) { namespaces.Add(serialWriter.MakePrefix(), Soap.XsdUrl); } }
private void SetNamespaces() { var result = new XmlSerializerNamespaces(); var resultingNamespaces = allNamespaces .ToArray().GroupBy(n => n.Namespace) .Select(g => g.OrderBy(n => string.IsNullOrWhiteSpace(n.Name)).First()).ToArray(); foreach (var resultingNamespace in resultingNamespaces) { result.Add( prefix: resultingNamespace.Name, ns: resultingNamespace.Namespace); if (defaultNamespace == default && string.IsNullOrWhiteSpace(resultingNamespace.Name)) { defaultNamespace = resultingNamespace.Namespace; } } namespaces = result; }
public static void WriteList <TItem>(string rootName, XmlSerializerNamespaces namespaces, IEnumerable <TItem> list, TextWriter textWriter) { var namespaceList = namespaces.ToArray(); string defaultNamespace = null; foreach (var ns in namespaceList) { if (string.IsNullOrEmpty(ns.Name)) { defaultNamespace = ns.Namespace; break; } } var settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = " "; using (var writer = XmlWriter.Create(textWriter, settings)) { writer.WriteStartDocument(); writer.WriteStartElement("RootLevel", defaultNamespace); foreach (var ns in namespaceList) { if (!string.IsNullOrEmpty(ns.Name)) { writer.WriteAttributeString("xmlns", ns.Name, null, ns.Namespace); } } var serializer = new XmlSerializer(typeof(TItem)); foreach (var item in list) { serializer.Serialize(writer, item); } writer.WriteEndElement(); writer.WriteEndDocument(); } }
public byte[] BuildBye(string[] scopes, bool onvif20, string[] xAddrs) { XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); return(BuildBye(scopes, BuildTypes(onvif20, ref namespaces), namespaces.ToArray(), xAddrs, 0)); }