System.Xml.XmlValidatingReader.GetAttribute C# (CSharp) Méthode

GetAttribute() public méthode

public GetAttribute ( int i ) : string
i int
Résultat string
        public override string GetAttribute( int i ) {
            return impl.GetAttribute( i );
        }

Same methods

XmlValidatingReader::GetAttribute ( string name ) : string
XmlValidatingReader::GetAttribute ( string localName, string namespaceURI ) : string

Usage Example

        /// <summary>
        /// takes in raw xml string and attempts to parse it into a workable hash.
        /// all valid xml for the gateway contains
        /// <transaction><fields><field key="attribute name">value</field></fields></transaction>
        /// there will be 1 or more (should always be more than 1 to be valid) field tags
        /// this method will take the attribute name and make that the hash key and then the value is the value 
        /// if an error occurs then the error key will be added to the hash.
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        private Hashtable parseXML(string xml)
        {
            Hashtable ret_hash = new Hashtable(); //stores key values to return
            XmlTextReader txtreader = null;
            XmlValidatingReader reader = null;

            if (xml != null && xml.Length > 0)
            {

                try
                {
                    //Implement the readers.
                    txtreader = new XmlTextReader(new System.IO.StringReader(xml));
                    reader = new XmlValidatingReader(txtreader);

                    //Parse the XML and display the text content of each of the elements.
                    while (reader.Read())
                    {
                        if (reader.IsStartElement() && reader.Name.ToLower() == "field")
                        {
                            if (reader.HasAttributes)
                            {
                                //we want the key attribute value
                                ret_hash[reader.GetAttribute(0).ToLower()] = reader.ReadString();
                            }
                            else
                            {
                                ret_hash["error"] = "All FIELD tags must contains a KEY attribute.";
                            }
                        }
                    } //ends while
                }
                catch (Exception e)
                {
                    //handle exceptions
                    ret_hash["error"] = e.Message;
                }
                finally
                {
                    if (reader != null) reader.Close();
                }
            }
            else
            {
                //incoming xml is empty
                ret_hash["error"] = "No data was present. Valid XML must be sent in order to process a transaction.";
            }

            return ret_hash;
        }