本文整理汇总了C#中System.Xml.Linq.XElement.GetNamespaceOfPrefix方法的典型用法代码示例。如果您正苦于以下问题:C# XElement.GetNamespaceOfPrefix方法的具体用法?C# XElement.GetNamespaceOfPrefix怎么用?C# XElement.GetNamespaceOfPrefix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XElement
的用法示例。
在下文中一共展示了XElement.GetNamespaceOfPrefix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetXName
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;
}
}
示例2: ResolveNamespace
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;
}
示例3: Initialize
public void Initialize(XElement element)
{
this.initializedAttributes = this.Attributes
.Select(_ => new AttributeSelector(
_.Name,
(element.GetNamespaceOfPrefix(_.Namespace)?.NamespaceName ?? _.Namespace),
_.Value))
.ToList();
}
示例4: Write
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);
}
示例5: LookupNamespace
public static Func<string, string> LookupNamespace(XElement element)
{
return prefix =>
{
var ns = element.GetNamespaceOfPrefix(prefix);
if (ns == null)
{
return null;
}
return ns.NamespaceName;
};
}
示例6: Parse
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")
};
}
示例7: ResolveNamespace
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);
}
示例8: DeserializeXElement
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);
}
示例9: ParseCseResult
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;
}
示例10: GetXmlNameFromString
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);
}
示例11: ResolvePrefix
private static XNamespace ResolvePrefix(XElement element, string prefix)
{
return string.IsNullOrEmpty(prefix) ? element.GetDefaultNamespace() : element.GetNamespaceOfPrefix(prefix);
}
示例12: TrySearchObject
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>();
}
}
示例13: ParseItem
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;
}
示例14: SearchObject
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>();
}
}
示例15: ElementGetNamespaceOfPrefix
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);
}