Finds an attribute with the given name in the given XML node and returns the attribute of the value. : Attribute « XML « C# / C Sharp






Finds an attribute with the given name in the given XML node and returns the attribute of the value.

 
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace JeffFerguson.Gepsio
{
    internal static class XmlUtilities
    {
        //------------------------------------------------------------------------------------
        // Finds an attribute with the given name in the given XML node and returns the
        // attribute of the value. Returns an empty string if no such attribute exists. Works
        // for local names as well as names qualified with a namespace identifier.
        //------------------------------------------------------------------------------------
        internal static string GetAttributeValue(XmlNode Node, string AttributeName)
        {
            bool NameIncludesNamespaceId;

            if (AttributeName.IndexOf(':') == -1)
                NameIncludesNamespaceId = false;
            else
                NameIncludesNamespaceId = true;
            if (Node == null)
                return string.Empty;
            if (Node.Attributes == null)
                return string.Empty;
            foreach (XmlAttribute CurrentAttribute in Node.Attributes)
            {
                if (NameIncludesNamespaceId == false)
                {
                    if (CurrentAttribute.LocalName.Equals(AttributeName) == true)
                        return CurrentAttribute.Value;
                }
                else
                {
                    if (CurrentAttribute.Name.Equals(AttributeName) == true)
                        return CurrentAttribute.Value;
                }
            }
            return string.Empty;
        }

        //------------------------------------------------------------------------------------
        // Finds an attribute with the given namespace URI and the given local name in the
        // given XML node and returns the attribute of the value. Returns an empty string if
        // no such attribute exists.
        //------------------------------------------------------------------------------------
        internal static string GetAttributeValue(XmlNode Node, string AttributeNamespaceUri, string AttributeLocalName)
        {
            if (Node == null)
                return string.Empty;
            if (Node.Attributes == null)
                return string.Empty; 
            foreach (XmlAttribute CurrentAttribute in Node.Attributes)
            {
                if (CurrentAttribute.NamespaceURI.Equals(AttributeNamespaceUri) == true)
                {
                    if (CurrentAttribute.LocalName.Equals(AttributeLocalName) == true)
                        return CurrentAttribute.Value;
                }
            }
            return string.Empty;
        }
    }
}

   
  








Related examples in the same category

1.Set Attribute Value
2.Get Attribute Value
3.Get Int value from xml attribute
4.Get Attribute value (2)
5.Get attribute or return default value
6.Get Bool Attribute Or Throw exception
7.Get attribute value and convert to integer type
8.Get attribute value and convert to integer type or throw exception
9.Get Attribute and return Int64
10.Get Attribute and return Int64 or throw exception
11.Adds an XmlAttribute to a XmlNode
12.Get boolean value if an attribute is 1 or true
13.Attempts to get and cast attribute from an XmlElement
14.Attempts to get and convert an attribute from an XmlElement
15.Gets a string from an attribute or node.
16.Remove all the xml namespaces (xmlns) attributes in the xml string
17.Get Attribute With Conversion
18.Find a single Attribute of the type 'attributeType' from the supplied class
19.Create Xml Attribute, Node
20.Create Attribute, Text Element, Image Element,Read Image Element
21.Attribute Util
22.Find and get Attribute value
23.Parse Attribute
24.Get attribute value (3)
25.Get int type attribute value
26.Get attribute value or throw exception
27.Read an XML file from the xml element and the xml attribute
28.Create and set attribute
29.Adds a new attribute to the given target element.
30.Get Text Attribute
31.Get Int Attribute