本文整理汇总了C#中System.Xml.Serialization.XmlSerializerNamespaces.LookupPrefix方法的典型用法代码示例。如果您正苦于以下问题:C# XmlSerializerNamespaces.LookupPrefix方法的具体用法?C# XmlSerializerNamespaces.LookupPrefix怎么用?C# XmlSerializerNamespaces.LookupPrefix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Serialization.XmlSerializerNamespaces
的用法示例。
在下文中一共展示了XmlSerializerNamespaces.LookupPrefix方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteStartElement
/// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.WriteStartElement5"]/*' />
protected void WriteStartElement(string name, string ns, object o, bool writePrefixed, XmlSerializerNamespaces xmlns) {
if (o != null && objectsInUse != null) {
if (objectsInUse.ContainsKey(o)) throw new InvalidOperationException(Res.GetString(Res.XmlCircularReference, o.GetType().FullName));
objectsInUse.Add(o, o);
}
string prefix = null;
bool needEmptyDefaultNamespace = false;
if (namespaces != null) {
foreach(string alias in namespaces.Namespaces.Keys) {
string aliasNs = (string)namespaces.Namespaces[alias];
if (alias.Length > 0 && aliasNs == ns)
prefix = alias;
if (alias.Length == 0) {
if (aliasNs == null || aliasNs.Length == 0)
needEmptyDefaultNamespace = true;
if (ns != aliasNs)
writePrefixed = true;
}
}
usedPrefixes = ListUsedPrefixes(namespaces.Namespaces, aliasBase);
}
if (writePrefixed && prefix == null && ns != null && ns.Length > 0) {
prefix = w.LookupPrefix(ns);
if (prefix == null || prefix.Length == 0) {
prefix = NextPrefix();
}
}
if (prefix == null && xmlns != null) {
prefix = xmlns.LookupPrefix(ns);
}
if (needEmptyDefaultNamespace && prefix == null && ns != null && ns.Length != 0)
prefix = NextPrefix();
w.WriteStartElement(prefix, name, ns);
if (namespaces != null) {
foreach(string alias in namespaces.Namespaces.Keys) {
string aliasNs = (string)namespaces.Namespaces[alias];
if (alias.Length == 0 && (aliasNs == null || aliasNs.Length == 0))
continue;
if (aliasNs == null || aliasNs.Length == 0) {
if (alias.Length > 0)
throw new InvalidOperationException(Res.GetString(Res.XmlInvalidXmlns, alias));
WriteAttribute("xmlns", alias, null, aliasNs);
}
else {
if (w.LookupPrefix(aliasNs) == null) {
// write the default namespace declaration only if we have not written it already, over wise we just ignore one provided by the user
if (prefix == null && alias.Length == 0)
break;
WriteAttribute("xmlns", alias, null, aliasNs);
}
}
}
}
WriteNamespaceDeclarations(xmlns);
}