private static void GetNameInAttributeScope(string qualifiedName, XElement e, out string localName, out string namespaceName) { if ((qualifiedName != null) && (qualifiedName.Length != 0)) { int index = qualifiedName.IndexOf(':'); if ((index != 0) && (index != (qualifiedName.Length - 1))) { if (index == -1) { localName = qualifiedName; namespaceName = string.Empty; return; } XNamespace namespaceOfPrefix = e.GetNamespaceOfPrefix(qualifiedName.Substring(0, index)); if (namespaceOfPrefix != null) { localName = qualifiedName.Substring(index + 1, (qualifiedName.Length - index) - 1); namespaceName = namespaceOfPrefix.NamespaceName; return; } } } localName = null; namespaceName = null; }
public XName GetXName(XElement elem, string reference) { string[] parts = reference.Split(':'); if (parts.Length == 2) { XNamespace ns = elem.GetNamespaceOfPrefix(parts[0]); if (ns != null) { return ns + parts[1]; } else { this.Importer.Diagnostics.AddError("Invalid namespace prefix: " + parts[0], this.Uri, this.GetTextSpan(elem)); return null; } } else if (parts.Length == 1) { XNamespace ns = elem.GetDefaultNamespace(); return ns + parts[0]; } else { return null; } }
public void ResolveNamespace(XElement elem, XamlContext ctx) { if (Namespace != null) return; // Since XmlnsProperty records are inside the element, // the namespace is resolved after processing the element body. string xmlNs = null; if (elem.Annotation<XmlnsScope>() != null) xmlNs = elem.Annotation<XmlnsScope>().LookupXmlns(Assembly, TypeNamespace); if (xmlNs == null) xmlNs = ctx.XmlNs.LookupXmlns(Assembly, TypeNamespace); if (xmlNs == null) { var nsSeg = TypeNamespace.Split('.'); var nsName = nsSeg[nsSeg.Length - 1].ToLowerInvariant(); var prefix = nsName; int count = 0; while (elem.GetNamespaceOfPrefix(prefix) != null) { count++; prefix = nsName + count; } xmlNs = string.Format("clr-namespace:{0};assembly={1}", TypeNamespace, Assembly); elem.Add(new XAttribute(XNamespace.Xmlns + XmlConvert.EncodeLocalName(prefix), ctx.GetXmlNamespace(xmlNs))); } Namespace = xmlNs; }
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; }
static void GetNameInAttributeScope(string qualifiedName, XElement e, out string localName, out string namespaceName) { if (qualifiedName != null && qualifiedName.Length != 0) { 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, qualifiedName.Length - i - 1); namespaceName = ns.NamespaceName; return; } } } localName = null; namespaceName = null; }
public void Initialize(XElement element) { this.initializedAttributes = this.Attributes .Select(_ => new AttributeSelector( _.Name, (element.GetNamespaceOfPrefix(_.Namespace)?.NamespaceName ?? _.Namespace), _.Value)) .ToList(); }
public static void Write(XElement container, KeyValuePair<string, object> kvp) { var element = new XElement(container.GetNamespaceOfPrefix("d") + kvp.Key);; if (kvp.Value == null) { element.SetAttributeValue(container.GetNamespaceOfPrefix("m") + "null", "true"); } else { var type = EdmType.FromSystemType(kvp.Value.GetType()); if (type != EdmType.String) { element.SetAttributeValue(container.GetNamespaceOfPrefix("m") + "type", type.ToString()); } element.SetValue(Writers[type](kvp.Value)); } container.Add(element); }
public static Func<string, string> LookupNamespace(XElement element) { return prefix => { var ns = element.GetNamespaceOfPrefix(prefix); if (ns == null) { return null; } return ns.NamespaceName; }; }
public static YouTubeChannelVideo Parse(XElement xItem) { // Get namespaces XNamespace atom = xItem.GetNamespaceOfPrefix("atom"); XNamespace media = xItem.GetNamespaceOfPrefix("media"); XNamespace yt = xItem.GetNamespaceOfPrefix("yt"); // Some pre-parsing XElement xMedia = xItem.Element(media + "group"); XElement xDuration = xMedia.Element(yt + "duration"); // Initialize and return the object return new YouTubeChannelVideo { Id = Regex.Match(xItem.GetElementValue("link"), "v=([v-]{11})").Groups[1].Value, Published = xItem.GetElementValue<DateTime>("pubDate"), LastUpdated = xItem.GetElementValue<DateTime>(atom + "updated"), Title = xItem.GetElementValue("title"), Description = xMedia.GetElementValue(media + "description"), Link = xItem.GetElementValue("link"), Author = xItem.GetElementValue("author"), Duration = xDuration.GetAttributeValue<int>("seconds") }; }
public override string LookupNamespace(string prefix) { if (EOF) { return(null); } XElement el = (node as XElement) ?? node.Parent; if (el == null) { return(null); } var xn = el.GetNamespaceOfPrefix(prefix); return(xn != XNamespace.None ? xn.NamespaceName : null); }
public void ResolveNamespace(XElement elem, XamlContext ctx) { if (Namespace != null) return; // Since XmlnsProperty records are inside the element, // the namespace is resolved after processing the element body. string xmlNs = null; if (elem.Annotation<XmlnsScope>() != null) xmlNs = elem.Annotation<XmlnsScope>().LookupXmlns(Assembly, TypeNamespace); if (xmlNs == null) xmlNs = ctx.XmlNs.LookupXmlns(Assembly, TypeNamespace); // Sometimes there's no reference to System.Xaml even if x:Type is used if (xmlNs == null) xmlNs = ctx.TryGetXmlNamespace(Assembly, TypeNamespace); if (xmlNs == null) { if (AssemblyNameComparer.CompareAll.Equals(Assembly, ctx.Module.Assembly)) xmlNs = $"clr-namespace:{TypeNamespace}"; else xmlNs = $"clr-namespace:{TypeNamespace};assembly={Assembly.Name}"; var nsSeg = TypeNamespace.Split('.'); var prefix = nsSeg[nsSeg.Length - 1].ToLowerInvariant(); if (string.IsNullOrEmpty(prefix)) { if (string.IsNullOrEmpty(TypeNamespace)) prefix = "global"; else prefix = "empty"; } int count = 0; var truePrefix = prefix; XNamespace prefixNs, ns = ctx.GetXmlNamespace(xmlNs); while ((prefixNs = elem.GetNamespaceOfPrefix(truePrefix)) != null && prefixNs != ns) { count++; truePrefix = prefix + count; } if (prefixNs == null) { elem.Add(new XAttribute(XNamespace.Xmlns + XmlConvert.EncodeLocalName(truePrefix), ns)); if (string.IsNullOrEmpty(TypeNamespace)) elem.AddBeforeSelf(new XComment(string.Format(dnSpy_BamlDecompiler_Resources.Msg_GlobalNamespace, truePrefix))); } } Namespace = ctx.GetXmlNamespace(xmlNs); }
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); }
private Collective SearchObject(ObjectTypes objType, string nameWithPrefix, XElement def) { // TODO: !!! MI VAN, HA NINCS PREFIX ??? string prefix = (string)nameWithPrefix.Split(char.Parse(":")).GetValue(0); XNamespace objNS = def.GetNamespaceOfPrefix(prefix); string name = (string)nameWithPrefix.Split(char.Parse(":")).GetValue(1); var objs = from c in readedElements where c.objectType == objType && c.name == name && c.ownNS == objNS select c; if (objs.Count<Collective>() != 1) { Wr("Searched Object not found: " + nameWithPrefix); throw new Exception(); } else { return objs.First<Collective>(); } }
private object DeserializeXElement(XElement e, object obj, Type defaultType, SerializationScope typeScope) { Debug.Assert(e != null && defaultType != null); // 对于集合,从元素名推理对象类型。 var xsiTypeName = (string)e.Attribute(XsiType); Type objType; if (xsiTypeName == null) { //未指定 xsi:type,则使用 defaultType objType = defaultType; } else { //指定了 xsi:type //解析 prefix:localName var parts = xsiTypeName.Split(':'); XName xName; if (parts.Length <= 1) xName = e.GetDefaultNamespace() + xsiTypeName; else xName = e.GetNamespaceOfPrefix(parts[0]) + parts[1]; objType = _Builder.GlobalScope.GetType(xName); if (objType == null) throw new NotSupportedException(string.Format(Prompts.UnregisteredType, xsiTypeName, typeScope)); } var s = _Builder.GetSerializer(objType); if (s == null) throw new NotSupportedException(string.Format(Prompts.UnregisteredType, objType, typeScope)); return s.Deserialize(e, obj, this, typeScope); }
private SoaMetaModel.Type SearchType(string nameWithPrefix, XElement def) { string prefix = (string)nameWithPrefix.Split(char.Parse(":")).GetValue(0); XNamespace objNS = def.GetNamespaceOfPrefix(prefix); string name = (string)nameWithPrefix.Split(char.Parse(":")).GetValue(1); if (objNS == xmlNS) { BuiltInType bit = BuiltInType.GetBuiltInType(name); return bit; } else { var objs = from c in readedElements where c.name == name && c.ownNS == objNS select c; if (objs.Count<Collective>() != 1) { Wr("Searched Type not found: " + nameWithPrefix); throw new Exception(); } else { return (SoaMetaModel.Type)objs.First<Collective>().objRef; } } }
private static XNamespace ResolvePrefix(XElement element, string prefix) { return string.IsNullOrEmpty(prefix) ? element.GetDefaultNamespace() : element.GetNamespaceOfPrefix(prefix); }
private SoaMetaModel.Type TrySearchType(string nameWithPrefix, XElement def) { if (nameWithPrefix == null) return null; if (!nameWithPrefix.Contains(":")) return null; string prefix = (string)nameWithPrefix.Split(char.Parse(":")).GetValue(0); XNamespace objNS = def.GetNamespaceOfPrefix(prefix); string name = (string)nameWithPrefix.Split(char.Parse(":")).GetValue(1); if (name.Equals("dateTime")) name = "DateTime"; if (name.Equals("boolean")) name = "bool"; if (objNS == xsdNS) { BuiltInType bit = BuiltInType.GetBuiltInType(name); return bit; } else { var objs = from c in store where c.name == name && c.ownNS == objNS && ( c.objectType == StoreItemType.arrayType || c.objectType == StoreItemType.enumType || c.objectType == StoreItemType.exceptionType || c.objectType == StoreItemType.structType) select c; if (objs.Count<StoreItem>() != 1) { return null; } else { return (SoaMetaModel.Type)objs.First<StoreItem>().objRef; } } }
public void ElementGetNamespaceOfPrefix() { XNamespace ns = XNamespace.Get("http://test"); XElement e = new XElement(ns + "foo"); Assert.Throws<ArgumentNullException>(() => e.GetNamespaceOfPrefix(null)); Assert.Throws<ArgumentException>(() => e.GetNamespaceOfPrefix(string.Empty)); XNamespace n = e.GetNamespaceOfPrefix("xmlns"); Assert.Equal("http://www.w3.org/2000/xmlns/", n.NamespaceName); n = e.GetNamespaceOfPrefix("xml"); Assert.Equal("http://www.w3.org/XML/1998/namespace", n.NamespaceName); n = e.GetNamespaceOfPrefix("myns"); Assert.Null(n); XDocument doc = new XDocument(e); e.SetAttributeValue("{http://www.w3.org/2000/xmlns/}myns", ns); n = e.GetNamespaceOfPrefix("myns"); Assert.NotNull(n); Assert.Equal(ns, n); }
private static XNamespace GetNamespaceOfKeyNamespace(XElement node) { return node.GetNamespaceOfPrefix(Constants.KeyAttribute.Split(Constants.Colon.ToCharArray())[0]); }
public override string LookupNamespace(string prefix) { if (this.IsInteractive) { if (prefix == null) { return(null); } XElement elementInScope = this.GetElementInScope(); if (elementInScope != null) { XNamespace namespace2 = (prefix.Length == 0) ? elementInScope.GetDefaultNamespace() : elementInScope.GetNamespaceOfPrefix(prefix); if (namespace2 != null) { return(this.nameTable.Add(namespace2.NamespaceName)); } } } return(null); }
private static XAttribute FindXAtt(XElement el, string name) { XNamespace ns = null; if (name.Contains(":")) { var strs = name.Split(':'); ns = el.GetNamespaceOfPrefix(strs[0]); name = strs[1]; } var selector = (ns != null) ? XName.Get(name, ns.ToString()) : name; return el.Attribute(selector); }
private static void Write(ISchema schema, string typeName, XElement root, XElement container, KeyValuePair<string, object> kvp) { var element = new XElement(root.GetNamespaceOfPrefix("d") + kvp.Key); if (kvp.Value == null) { element.SetAttributeValue(container.GetNamespaceOfPrefix("m") + "null", "true"); } else { var property = root == container ? schema .EntityTypes.Single(x => x.Name == typeName) .Properties.Single(x => x.Name == kvp.Key) : schema .ComplexTypes.Single(x => x.Name == typeName) .Properties.Single(x => x.Name == kvp.Key); if (property.Type is EdmComplexPropertyType) { if (kvp.Value.ToString() == string.Empty) { element.SetAttributeValue(container.GetNamespaceOfPrefix("m") + "null", "true"); } else { var edmType = (property.Type as EdmComplexPropertyType).Type; element.SetAttributeValue(root.GetNamespaceOfPrefix("m") + "type", edmType.Name); foreach (var prop in kvp.Value as IDictionary<string, object>) { Write(schema, property.Type.Name, root, element, prop); } } } else if (property.Type is EdmPrimitivePropertyType) { var edmType = (property.Type as EdmPrimitivePropertyType).Type; if (edmType != EdmType.String) { element.SetAttributeValue(root.GetNamespaceOfPrefix("m") + "type", edmType.Name); if (kvp.Value.ToString() == string.Empty) { element.SetAttributeValue(container.GetNamespaceOfPrefix("m") + "null", "true"); } } element.SetValue(Writers[edmType](kvp.Value)); } else if (property.Type is EdmEnumPropertyType) { var edmType = (property.Type as EdmEnumPropertyType).Type; element.SetAttributeValue(root.GetNamespaceOfPrefix("m") + "type", edmType.Name); if (kvp.Value.ToString() == string.Empty) { element.SetAttributeValue(container.GetNamespaceOfPrefix("m") + "null", "true"); } element.SetValue(kvp.Value); } } container.Add(element); }
private StoreItem TrySearchObject(StoreItemType objType, string nameWithPrefix, XElement def) { if (nameWithPrefix == null) return null; if (!nameWithPrefix.Contains(":")) return null; string prefix = (string)nameWithPrefix.Split(char.Parse(":")).GetValue(0); XNamespace objNS = def.GetNamespaceOfPrefix(prefix); string name = (string)nameWithPrefix.Split(char.Parse(":")).GetValue(1); var objs = from c in store where c.objectType == objType && c.name == name && c.ownNS == objNS select c; if (objs.Count<StoreItem>() != 1) { return null; } else { return objs.First<StoreItem>(); } }
protected virtual BaseFeedItem ParseRss20SingleItem(XElement itemNode) { var titleNode = itemNode.Element("title"); var datePublishedNode = itemNode.Element("pubDate"); var authorNode = itemNode.Element("author"); var commentsNode = itemNode.Element("comments"); var idNode = itemNode.Element("guid"); var contentNode = itemNode.Element("description"); var linkNode = itemNode.Element("link"); /* content:encoded category itunes:duration itunes:subtitle itunes:summary */ var dcCreatorNode = (itemNode.GetNamespaceOfPrefix("dc") != null ? itemNode.Element(itemNode.GetNamespaceOfPrefix("dc") + "creator") : null); // itemNode.Element("dc:creator"); var contentEncodedNode = (itemNode.GetNamespaceOfPrefix("content") != null ? itemNode.Element(itemNode.GetNamespaceOfPrefix("content") + "encoded") : null); //itemNode.Element("content:encoded"); var enclosureNode = itemNode.Element("enclosure"); PodcastRss20FeedItem item = CreatePodcastRss20FeedItem(); item.Title = titleNode == null ? string.Empty : titleNode.Value; item.DatePublished = datePublishedNode == null ? DateTime.UtcNow : SafeGetDate(datePublishedNode.Value); item.Author = authorNode == null ? string.Empty : authorNode.Value; item.Comments = commentsNode == null ? string.Empty : commentsNode.Value; item.Id = idNode == null ? string.Empty : idNode.Value; item.Content = contentNode == null ? string.Empty : contentNode.Value; item.Link = linkNode == null ? string.Empty : linkNode.Value; item.DcCreator = dcCreatorNode == null ? string.Empty : dcCreatorNode.Value; //item.ContentEncoded = contentEncodedNode == null ? string.Empty : contentEncodedNode.Value; item.Enclosure = enclosureNode == null ? string.Empty : enclosureNode.Value; var categoryNodes = itemNode.Elements("category"); foreach (var categoryNode in categoryNodes) { item.Categories.Add(categoryNode.Value); } return item; }
private static YouTubeVideo ParseEntry(XElement xEntry) { // Get namespaces XNamespace atom = xEntry.GetDefaultNamespace(); XNamespace media = xEntry.GetNamespaceOfPrefix("media"); XNamespace yt = xEntry.GetNamespaceOfPrefix("yt"); XNamespace gd = xEntry.GetNamespaceOfPrefix("gd"); // Some pre-parsing XElement xAuthor = xEntry.Element(atom + "author"); XElement xMedia = xEntry.Element(media + "group"); XElement xDuration = xMedia.Element(yt + "duration"); XElement xLink = xEntry.Elements(atom + "link").FirstOrDefault(x => x.GetAttributeValue("rel") == "alternate"); // Get the <yt:statistics> element describing the number of favorites and // number of views XElement xStatistics = xEntry.Element(yt + "statistics"); // Get the <yt:rating> element describing the number of lines and // number of dislikes (may not always be present) XElement xYouTubeRating = xEntry.Element(yt + "rating"); // Get the <gd:rating> element describing the video rating and // number of raters XElement xGoogleRating = xEntry.Element(gd + "rating"); // Initialize and return the object YouTubeVideo video = new YouTubeVideo(); video.Id = xLink == null ? null : Regex.Match(xLink.GetAttributeValue("href"), "v=([\\w-]{11})").Groups[1].Value; video.Published = xEntry.GetElementValue<DateTime>(atom + "published"); video.LastUpdated = xEntry.GetElementValue<DateTime>(atom + "updated"); video.Title = xEntry.GetElementValue(atom + "title"); video.Description = xMedia.GetElementValue(media + "description"); video.Link = xLink == null ? null : xLink.GetAttributeValue("href"); video.Author = xAuthor.GetElementValue(atom + "name"); video.Duration = TimeSpan.FromSeconds(xDuration.GetAttributeValue<int>("seconds")); video.FavoriteCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue<int>("favoriteCount"); video.ViewCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue<int>("viewCount"); video.Likes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue<int>("numLikes"); video.Dislikes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue<int>("numDislikes"); video.Rating = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue<double>("average", CultureInfo.InvariantCulture); video.NumberOfRaters = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue<int>("numRaters"); return video; }
private GoogleSearchResult ParseCseResult(XElement searchResult) { var nsCse = searchResult.GetNamespaceOfPrefix("cse"); var nsAtom = searchResult.GetDefaultNamespace(); var tmpResult = new GoogleSearchResult(); var title = searchResult.Element(nsAtom + "title"); if (title != null) tmpResult.Title = title.Value; var link = searchResult.Element(nsAtom + "link"); if (link != null) { if (link.Attribute("href") != null) { tmpResult.Url = link.Attribute("href").Value; } } var description = searchResult.Element(nsAtom + "summary"); if (description != null) { tmpResult.Description = description.Value; } var mime = searchResult.Element(nsCse + "mime"); if (mime != null) { tmpResult.Mime = mime.Value; } return tmpResult; }
private static YouTubeVideo ParseItem(XElement xItem) { // Get namespaces XNamespace atom = xItem.GetNamespaceOfPrefix("atom"); XNamespace media = xItem.GetNamespaceOfPrefix("media"); XNamespace yt = xItem.GetNamespaceOfPrefix("yt"); XNamespace gd = xItem.GetNamespaceOfPrefix("gd"); // Some pre-parsing XElement xMedia = xItem.Element(media + "group"); XElement xDuration = xMedia.Element(yt + "duration"); // Get the <yt:statistics> element describing the number of favorites and // number of views XElement xStatistics = xItem.Element(yt + "statistics"); // Get the <yt:rating> element describing the number of lines and // number of dislikes (may not always be present) XElement xYouTubeRating = xItem.Element(yt + "rating"); // Get the <gd:rating> element describing the video rating and // number of raters XElement xGoogleRating = xItem.Element(gd + "rating"); // Initialize and return the object YouTubeVideo video = new YouTubeVideo(); video.Id = GetVideoId(xItem); video.Published = xItem.GetElementValue<DateTime>("pubDate"); video.LastUpdated = xItem.GetElementValue<DateTime>(atom + "updated"); video.Title = xItem.GetElementValue("title"); video.Description = xItem.GetElementValue("description"); video.Link = xItem.GetElementValue("link"); video.Author = xItem.GetElementValue("author"); video.Duration = TimeSpan.FromSeconds(xDuration.GetAttributeValue<int>("seconds")); video.FavoriteCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue<int>("favoriteCount"); video.ViewCount = xStatistics == null ? 0 : xStatistics.GetAttributeValue<int>("viewCount"); video.Likes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue<int>("numLikes"); video.Dislikes = xYouTubeRating == null ? 0 : xYouTubeRating.GetAttributeValue<int>("numDislikes"); video.Rating = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue<int>("average"); video.NumberOfRaters = xGoogleRating == null ? 0 : xGoogleRating.GetAttributeValue<int>("numRaters"); return video; }
/// <summary> /// Tests XElement.GetNamespaceOfPrefix(). /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "ElementGetNamespaceOfPrefix")] public void ElementGetNamespaceOfPrefix() { XNamespace ns = XNamespace.Get("http://test"); XElement e = new XElement(ns + "foo"); try { e.GetNamespaceOfPrefix(null); Validate.ExpectedThrow(typeof(ArgumentException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } try { e.GetNamespaceOfPrefix(string.Empty); Validate.ExpectedThrow(typeof(ArgumentException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentException)); } XNamespace n = e.GetNamespaceOfPrefix("xmlns"); Validate.String(n.NamespaceName, "http://www.w3.org/2000/xmlns/"); n = e.GetNamespaceOfPrefix("xml"); Validate.String(n.NamespaceName, "http://www.w3.org/XML/1998/namespace"); n = e.GetNamespaceOfPrefix("myns"); Validate.IsNull(n); XDocument doc = new XDocument(e); e.SetAttributeValue("{http://www.w3.org/2000/xmlns/}myns", ns); n = e.GetNamespaceOfPrefix("myns"); Validate.IsEqual(n, ns); }
internal static XName GetXmlNameFromString(string value, XElement parent) { Debug.Assert(parent!=null); if (parent==null) throw new ArgumentNullException("parent"); string[] ne=value.Split(new char[] { ':' }, 2); string name=ne[0]; if (ne.Length>1) { name=ne[1]; XNamespace @namespace=parent.GetNamespaceOfPrefix(ne[0]); if (@namespace==null) throw new XmlException( string.Format( CultureInfo.CurrentCulture, SR.CouldNotFindNamespaceFromPrefix, ne[0] ) ); return XName.Get(name, @namespace.NamespaceName); } return XName.Get(name); }
private void AddNamespacesToElement(XElement rootNode) { var nsNoPrefix = new List<XNamespace>(); foreach (var ns in m_namespaceToPrefix.Keys) { string prefix = m_namespaceToPrefix[ns]; if (String.IsNullOrEmpty(prefix)) { nsNoPrefix.Add(ns); } else // if it has a prefix assigned { // if no namespace with this prefix already exists if (rootNode.GetNamespaceOfPrefix(prefix) == null) { rootNode.AddAttributeNamespaceSafe(XNamespace.Xmlns + prefix, ns); } else // if this prefix is already added { // check the namespace associated with this prefix var existing = rootNode.GetNamespaceOfPrefix(prefix); if (existing != ns) throw new InvalidOperationException(String.Format("You cannot have two different namespaces with the same prefix." + Environment.NewLine + "Prefix: {0}, Namespaces: \"{1}\", and \"{2}\"", prefix, ns, existing)); } } } // if the main type wrapper has a default (no prefix) namespace if (m_udtWrapper.Namespace.HasNamespace() && String.IsNullOrEmpty(m_udtWrapper.NamespacePrefix)) { // it will be added automatically nsNoPrefix.Remove(m_udtWrapper.Namespace); } // now generate namespaces for those without prefix foreach (var ns in nsNoPrefix) { rootNode.AddAttributeNamespaceSafe(XNamespace.Xmlns + rootNode.GetRandomPrefix(), ns); } }
private static XNamespace ResolvePrefix(XElement element, string prefix) { return(string.IsNullOrEmpty(prefix) ? element.GetDefaultNamespace() : element.GetNamespaceOfPrefix(prefix)); }