本文整理汇总了C#中System.Xml.Linq.XName.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# XName.ToString方法的具体用法?C# XName.ToString怎么用?C# XName.ToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XName
的用法示例。
在下文中一共展示了XName.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryGetAttribute
public static XAttribute TryGetAttribute(this XElement element, XName attributeName)
{
var attribute = element.Attribute(attributeName) ??
element.Attributes().SingleOrDefault(
a => a.Name.ToString().Homogenize() == attributeName.ToString().Homogenize());
return attribute ?? new XAttribute(attributeName, string.Empty);
}
示例2: TryGetAttributeValue
public static string TryGetAttributeValue(this XElement element, XName attributeName)
{
var attribute = element.Attribute(attributeName) ??
element.Attributes().SingleOrDefault(
a => a.Name.ToString().Homogenize() == attributeName.ToString().Homogenize());
return attribute == null ? null : attribute.Value;
}
示例3: AddAttribute
/// <summary>
/// Adds a new attribute to the element
/// Does not permit modification of an existing attribute.
/// Does not add empty or null attributes or values.
/// </summary>
/// <param name="element">The element to add the attribute to</param>
/// <param name="attribute">The attribute to add</param>
/// <param name="value">the value of the attribute to add</param>
/// <returns>The element passed in. (Permits fluent usage)</returns>
internal static XElement AddAttribute(this XElement element, XName attribute, string value) {
if (element == null) {
return null;
}
// we quietly ignore attempts to add empty data or attributes.
if (string.IsNullOrWhiteSpace(value) || attribute == null || string.IsNullOrWhiteSpace(attribute.ToString())) {
return element;
}
// Swidtag attributes can be added but not changed -- if it already exists, that's not permitted.
var current = element.GetAttribute(attribute);
if (!string.IsNullOrWhiteSpace(current)) {
if (value != current) {
throw new Exception("Attempt to change Attribute '{0}' present in element '{1}'".format(attribute.LocalName, element.Name.LocalName));
}
// if the value was set to that already, don't worry about it.
return element;
}
element.SetAttributeValue(attribute, value);
return element;
}
示例4: ShouldEqual
public static void ShouldEqual(this XName actual, XName expected)
{
if (actual.ToString() != expected.ToString())
{
throw new EqualException(expected, actual);
}
}
示例5: GetAttribute
/// <summary>
/// Gets the attribute value for a given element.
/// </summary>
/// <param name="element">the element that possesses the attribute</param>
/// <param name="attribute">the attribute to find</param>
/// <returns>the string value of the element. Returns null if the element or attribute does not exist.</returns>
internal static string GetAttribute(this XElement element, XName attribute) {
if (element == null || attribute == null || string.IsNullOrWhiteSpace(attribute.ToString())) {
return null;
}
var a = element.Attribute(attribute);
return a == null ? null : a.Value;
}
示例6: XsltContextFunctionAttribute
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="name"></param>
public XsltContextFunctionAttribute(XName name)
: base(typeof(IXsltContextFunction))
{
Contract.Requires<ArgumentNullException>(name != null);
this.expandedName = name.ToString();
}
示例7: FormatName
public static string FormatName(this XElement element, XName name)
{
if (name.Namespace == null) return name.LocalName;
if (name.Namespace == element.GetDefaultNamespace()) return name.LocalName;
string prefix = element.GetPrefixOfNamespace(name.Namespace);
if (!string.IsNullOrEmpty(prefix)) return prefix + ":" + name.LocalName;
return name.ToString();
}
示例8: AreEqual
private static bool AreEqual(XName left, XName right)
{
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
return left.ToString() == right.ToString();
}
示例9: Descendants
/// <summary>
/// Returns a collection of the descendant elements for this document or element, in document order. Optionally ignores case.
/// </summary>
/// <param name="element"></param>
/// <param name="name"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static IEnumerable<XElement> Descendants( this XElement element, XName name, bool ignoreCase )
{
var collection = element.Descendants();
if ( ignoreCase )
{
collection = collection.Where( p => p.Name.ToString().ToLower() == name.ToString().ToLower() );
}
return collection;
}
示例10: Attributes
/// <summary>
/// Returns a filtered collection of the attributes of every element in the source
/// collection. Only elements that have a matching <see cref="System.Xml.Linq.XName"/> are
/// included in the collection.
/// </summary>
/// <param name="elements"></param>
/// <param name="name"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static IEnumerable<XAttribute> Attributes( this IEnumerable<XElement> elements, XName name, bool ignoreCase )
{
if ( ignoreCase )
{
return elements.Attributes().Where( p => p.Name.ToString().ToLower() == name.ToString().ToLower() );
}
else
{
return elements.Attributes( name );
}
}
示例11: Attribute
/// <summary>
/// Returns the <see cref="System.Xml.Linq.XAttribute"/> of this <see cref="System.Xml.Linq.XElement"/> that
/// has the specified <see cref="System.Xml.Linq.XName"/>, optionally ignoring case.
/// </summary>
/// <param name="element"></param>
/// <param name="name"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static XAttribute Attribute( this XElement element, XName name, bool ignoreCase )
{
if ( ignoreCase )
{
return element.Attributes().Where( p => p.Name.ToString().ToLower() == name.ToString().ToLower() ).FirstOrDefault();
}
else
{
return element.Attribute( name );
}
}
示例12: Element
public static XElement Element(this XElement element, XName name, bool ignoreCase)
{
var el = element.Element(name);
if (el != null)
return el;
if (!ignoreCase)
return null;
var elements = element.Elements().Where(e => e.Name.LocalName.ToString().ToLowerInvariant() == name.ToString().ToLowerInvariant());
return elements.Count() == 0 ? null : elements.First();
}
示例13: Attribute
// ignore case if set
public static XAttribute Attribute( this XElement element, XName name, bool ignoreCase )
{
var at = element.Attribute( name );
if (at != null)
return at;
if (!ignoreCase)
return null;
var ats = element.Attributes().Where( e => e.Name.LocalName.ToString().ToLowerInvariant() == name.ToString().ToLowerInvariant() );
return ats.Count() == 0 ? null : ats.First();
}
示例14: GetAttribute
/// <summary>
/// Gets the attribute value for a given element.
/// </summary>
/// <param name="element">the element that possesses the attribute</param>
/// <param name="attribute">the attribute to find</param>
/// <returns>the string value of the element. Returns null if the element or attribute does not exist.</returns>
internal static string GetAttribute(this XElement element, XName attribute) {
if (element == null || attribute == null || string.IsNullOrWhiteSpace(attribute.ToString())) {
return null;
}
XAttribute result;
// no name space, just check local name
if (string.IsNullOrWhiteSpace(attribute.Namespace.NamespaceName))
{
result = element.Attributes().Where(attr => attr != null && string.Equals(attr.Name.LocalName, attribute.LocalName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
}
else
{
result = element.Attribute(attribute);
}
return result == null ? null : result.Value;
}
示例15: SetAttribute
/// <summary>
/// Adds a new attribute to the element
/// Does not permit modification of an existing attribute.
/// Does not add empty or null attributes or values.
/// </summary>
/// <param name="element">The element to add the attribute to</param>
/// <param name="attribute">The attribute to add</param>
/// <param name="value">the value of the attribute to add</param>
/// <returns>The element passed in. (Permits fluent usage)</returns>
internal static XElement SetAttribute(this XElement element, XName attribute, string value) {
if (element == null) {
return null;
}
// we quietly ignore attempts to add empty data or attributes.
if (attribute == null || string.IsNullOrWhiteSpace(attribute.ToString())) {
return element;
}
if (element.Name.Namespace == attribute.Namespace || string.IsNullOrWhiteSpace(attribute.NamespaceName) || attribute.Namespace == Namespace.XmlNs || attribute.Namespace == Namespace.Xml) {
element.SetAttributeValue(attribute.LocalName, value);
} else {
element.EnsureNamespaceAtTop(attribute.Namespace);
element.SetAttributeValue(attribute, value);
}
return element;
}