private void LoadNamespaces(XElement source, string assembly, string originFilePath, bool addToRoot) { var nsDeclarations = source.Attributes().Where(attr => attr.IsNamespaceDeclaration); var clrNamespaceHelper = new ClrNamespaceHelper(); foreach (var declaration in nsDeclarations) { var uri = clrNamespaceHelper.NormalizeNamespaceUri(declaration.Value, assembly); var info = new NamespaceInfo { Prefix = GetNsPrefix(declaration), Uri = uri, OriginFilePath = originFilePath }; NamespaceInfo existing; if (_namespaces.TryGetValue(info.Prefix, out existing)) { if (existing.Uri != info.Uri) { throw new ClashException(existing, info); } } else { _namespaces[info.Prefix] = info; if (addToRoot) { // cannot add new "xmlns" delaration to root element, this would change namespaces of all elements in the document if (info.Prefix == "") { var defaultNamespace = _root.GetDefaultNamespace(); if (info.Uri != defaultNamespace) { info.Prefix = "xmlns"; throw new ClashException(GetDefaultNamespaceInfo(defaultNamespace.NamespaceName), info); } } else { _root.Add(new XAttribute(XNamespace.Xmlns + info.Prefix, info.Uri)); } } } } }
public XamlFile Read(string path) { try { Path = path; if (Assembly == null) { Xml = _fs.ReadXml(path); } else { var content = _fs.ReadAllText(path); var normalized = new ClrNamespaceHelper().NormalizeLocalNamespaces(content, Assembly); Xml = XDocument.Parse(normalized); } return(this); } catch (Exception ex) { throw new InvalidOperationException($"Error reading XAML from '{path}': {ex.Message}", ex); } }