/// <summary> /// Initializes a new instance of the <see cref="XmlConfiguration" /> class. /// </summary> /// <param name="tracer">The tracer.</param> /// <param name="reader">The reader providing the XML stream.</param> public XmlConfiguration(ITracer tracer, TextReader?reader) { XNamespace? @namespace = null; XDocument? document = null; if ((reader != null) && (reader.Peek() != -1)) { try { document = XDocument.Load(reader, LoadOptions.PreserveWhitespace); var root = document.Root; if (root != null) { @namespace = root.GetDefaultNamespace(); } } catch (Exception ex) { tracer.TraceError(ex.ToString()); } } if ((@namespace == null) || !DefaultNamespace.Equals(@namespace.NamespaceName, StringComparison.Ordinal)) { @namespace = XNamespace.Get(DefaultNamespace); var root = new XElement(XName.Get("Values", @namespace.NamespaceName)); document = new XDocument(root); } _document = document ?? new XDocument(); _root = _document.Root; _keyName = XName.Get("Key"); _valueName = XName.Get("Value", @namespace.NamespaceName); }
private static void GetNameInAttributeScope(string?qualifiedName, XElement e, out string?localName, out string?namespaceName) { if (!string.IsNullOrEmpty(qualifiedName)) { int i = qualifiedName.IndexOf(':'); if (i != 0 && i != qualifiedName.Length - 1) { if (i == -1) { localName = qualifiedName; namespaceName = string.Empty; return; } XNamespace?ns = e.GetNamespaceOfPrefix(qualifiedName.Substring(0, i)); if (ns != null) { localName = qualifiedName.Substring(i + 1); namespaceName = ns.NamespaceName; return; } } } localName = null; namespaceName = null; }
/// <summary> /// Ensure that an XNamespace object for 'namespaceName' has been atomically created. In other words, all outstanding /// references to this particular namespace, on any thread, must all be to the same object. Care must be taken, /// since other threads can be concurrently calling this method, and the target of a WeakReference can be cleaned up /// at any time by the GC. /// </summary> private static XNamespace EnsureNamespace(ref WeakReference?refNmsp, string namespaceName) { WeakReference?refOld; // Keep looping until a non-null namespace has been retrieved while (true) { // Save refNmsp in local variable, so we can work on a value that will not be changed by another thread refOld = refNmsp; if (refOld != null) { // If the target of the WeakReference is non-null, then we're done--just return the value XNamespace?ns = (XNamespace?)refOld.Target; if (ns != null) { return(ns); } } // Either refNmsp is null, or its target is null, so update it // Make sure to do this atomically, so that we can guarantee atomicity of XNamespace objects Interlocked.CompareExchange(ref refNmsp, new WeakReference(new XNamespace(namespaceName)), refOld); } }
public XmlElementContext(SerializationContext original, XDeclaration?declaration, XNamespace?xNamespace, string rootName) : base(original) { XDocument?doc = null; XElement? ele = null; switch (Argument.NotNull(original, nameof(original)).SerializerMode) { case SerializerMode.Deserialize: doc = XDocument.Load(TextReader, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo); break; case SerializerMode.Serialize: if (declaration != null) { doc = new XDocument(declaration, xNamespace != null ? new XElement(xNamespace + rootName) : new XElement(rootName)); _currentElement = doc; } else { ele = new XElement(rootName); _currentElement = ele; } break; default: throw new ArgumentOutOfRangeException(nameof(original)); } XElement = (ele ?? doc?.Root) ?? throw new InvalidOperationException("XElement is Null"); }
public InternalXmlSerializer(ObjectBuilder builder, SimpleMapper <XmlElementContext> mapper, string rootName, XDeclaration?declaration, XNamespace?xNamespace) : base(builder, mapper, ContextMode.Text) { _rootName = Argument.NotNull(rootName, nameof(rootName)); _declaration = declaration; _xNamespace = xNamespace; }
public XmlSerializerConfiguration(string rootName, XDeclaration?declaration, XNamespace?xNamespace, [NotNull] Type targetType) { _rootName = Argument.NotNull(rootName, nameof(rootName)); _declaration = declaration; _xNamespace = xNamespace; _targetType = Argument.NotNull(targetType, nameof(targetType)); _builder = new ObjectBuilder(_targetType); }
public Fb2NodeMetadata( XNamespace?defaultNamespace = null, IEnumerable <XAttribute>?namespaceDeclarations = null) { if (defaultNamespace != null) { DefaultNamespace = defaultNamespace; } if (namespaceDeclarations != null && namespaceDeclarations.Any()) { var namespaceDeclarationsOnly = namespaceDeclarations.All(attr => attr.IsNamespaceDeclaration); if (!namespaceDeclarationsOnly) { throw new ArgumentException($"{nameof(namespaceDeclarations)} should contain Namespace Declarations attributes only."); } NamespaceDeclarations = namespaceDeclarations; } }
public override string?LookupNamespace(string prefix) { if (!IsInteractive) { return(null); } if (prefix == null) { return(null); } XElement?e = GetElementInScope(); if (e != null) { XNamespace?ns = prefix.Length == 0 ? e.GetDefaultNamespace() : e.GetNamespaceOfPrefix(prefix); if (ns != null) { return(_nameTable.Add(ns.NamespaceName)); } } return(null); }