ReadString() public method

public ReadString ( ) : string
return string
Esempio n. 1
0
		public void TestExpandEntity ()
		{
			string intSubset = "<!ELEMENT root (#PCDATA)><!ATTLIST root foo CDATA 'foo-def' bar CDATA 'bar-def'><!ENTITY ent 'entity string'>";
			string dtd = "<!DOCTYPE root [" + intSubset + "]>";
			string xml = dtd + "<root foo='&ent;' bar='internal &ent; value'>&ent;</root>";
			dvr = PrepareXmlReader (xml);
			dvr.EntityHandling = EntityHandling.ExpandEntities;
			dvr.Read ();	// DTD
			dvr.Read ();
			Assert.AreEqual (XmlNodeType.Element, dvr.NodeType);
			Assert.AreEqual ("root", dvr.Name);
			Assert.IsTrue (dvr.MoveToFirstAttribute ());
			Assert.AreEqual ("foo", dvr.Name);
			Assert.AreEqual ("entity string", dvr.Value);
			Assert.IsTrue (dvr.MoveToNextAttribute ());
			Assert.AreEqual ("bar", dvr.Name);
			Assert.AreEqual ("internal entity string value", dvr.Value);
			Assert.AreEqual ("entity string", dvr.ReadString ());

			// ValidationType = None

			dvr = PrepareXmlReader (xml);
			dvr.EntityHandling = EntityHandling.ExpandEntities;
			dvr.ValidationType = ValidationType.None;
			dvr.Read ();	// DTD
			dvr.Read ();
			Assert.AreEqual (XmlNodeType.Element, dvr.NodeType);
			Assert.AreEqual ("root", dvr.Name);

			Assert.IsTrue (dvr.MoveToFirstAttribute ());
			Assert.AreEqual ("foo", dvr.Name);
			Assert.AreEqual ("entity string", dvr.Value);

			Assert.IsTrue (dvr.MoveToNextAttribute ());
			Assert.AreEqual ("bar", dvr.Name);
			Assert.AreEqual ("internal entity string value", dvr.Value);
			Assert.AreEqual ("entity string", dvr.ReadString ());
		}
        /// <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;
        }