// Conversion of an XML-structure (in a string or in a file) to a Prolog BaseTerm
            public static BaseTerm XmlToTerm(BaseTerm settings, string s, bool inFile)
            {
                XmlTextReader xrd = null;
                StreamReader sr = null;
                Encoding encoding = GetEncodingFromString ("UTF-8");
                Node result;
                string settingValue = null;

                // get settings -- currently, only 'encoding' is recognized
                if (settings != null)
                  foreach (BaseTerm setting in (ListTerm)settings) // traverse ...
                  {
                string settingName = setting.FunctorToString;

                if (setting.Arity == 1)
                  settingValue = setting.Arg (0).FunctorToString;
                else
                  IO.Error ("Illegal setting in xml_term/3: '{0}'", setting);

                switch (settingName)
                {
                  // Expected string or file encoding. Superseded by explicit encoding attribute setting found in xml
                  case "encoding":
                encoding = GetEncodingFromString (settingValue); // default is UTF-8
                break;
                  default:
                IO.Error ("Unknown setting in xml_term/3: '{0}'", setting);
                break;
                }
                  }

                try
                {
                  if (inFile)
                  {
                sr = new StreamReader (s, encoding);
                xrd = new XmlTextReader (sr);
                  }
                  else
                xrd = new XmlTextReader (new StringReader (s));

                  //xrd.ProhibitDtd = true; // new in the .NET Framework version 2.0
                  xrd.Namespaces = false;
                  result = new Node ();
                  result.TagName = "<root>";
                  result.type = XmlNodeType.Element;

                  result.ToNode (xrd, 0); // first, create an intermediate representation (a Node) containing the XML structure
                }
                catch (Exception e)
                {
                  string source = inFile ? string.Format (" file '{0}'", s) : null;

                  throw new ApplicationException (
                string.Format ("Error in XML input{0}. Message was:\r\n{1}", source, e.Message));
                }
                finally
                {
                  if (sr != null) sr.Close ();
                  if (xrd != null) xrd.Close ();
                }

                return result.ToTerm (); // Convert the Node to a Prolog BaseTerm
            }
예제 #2
0
        /// Conversion of an XML-structure (in a string or in a file) to a Prolog Term
        public static Term XmlToTerm(string s, bool inFile)
        {
            XmlTextReader xrd;

              if (inFile)
            xrd = new XmlTextReader(s);
              else
            xrd = new XmlTextReader(new StringReader(s));

              //xrd.ProhibitDtd = true; // new in the .NET Framework version 2.0
              Node result = new Node();
              result.TagName = "<root>";
              result.type = XmlNodeType.Element;
              try
              {
            result.ToNode(xrd, 0); // first, create an intermediate representation (a Node) containing the XML structure
              }
              finally
              {
            xrd.Close();
              }
              return result.ToTerm(); // Convert the Node to a Prolog Term
        }