// 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 }
public void ToNode(XmlTextReader reader, int level) { Node child; // essentially: in the loop, add new nodes to this.childNodes while (reader.Read ()) { //IO.WriteLine ("Read name={0} value={1} reader.NodeType={2} Level={3}", reader.Name, reader.Value.Trim (), reader.NodeType, level); switch (reader.NodeType) { case XmlNodeType.Element: // create a new subelement bool isEmpty = reader.IsEmptyElement; child = new Node (); child.type = XmlNodeType.Element; child.name = reader.LocalName; child.text = reader.Prefix; while (reader.MoveToNextAttribute ()) child.AddAttribute (reader.Name, reader.Value); if (isEmpty) { childNodes.Insert (0, child); continue; } child.ToNode (reader, level + 1); childNodes.Insert (0, child); break; case XmlNodeType.Attribute: this.AddAttribute (reader.Name, reader.Value); break; case XmlNodeType.EndElement: return; case XmlNodeType.Comment: case XmlNodeType.Text: case XmlNodeType.CDATA: child = new Node (); child.type = reader.NodeType; child.Text = reader.Value; childNodes.Insert (0, child); break; case XmlNodeType.ProcessingInstruction: child = new Node (); child.type = reader.NodeType; child.name = reader.Name; child.text = reader.Value; childNodes.Insert (0, child); break; case XmlNodeType.XmlDeclaration: while (reader.MoveToNextAttribute ()) this.AddAttribute (reader.Name, reader.Value); break; case XmlNodeType.Document: case XmlNodeType.DocumentFragment: case XmlNodeType.DocumentType: case XmlNodeType.EndEntity: case XmlNodeType.Entity: case XmlNodeType.EntityReference: case XmlNodeType.None: case XmlNodeType.Notation: case XmlNodeType.SignificantWhitespace: case XmlNodeType.Whitespace: // ignore break; default: throw new ApplicationException ( string.Format ("*** Unhandled XmlTextReader.NodeType: {0}", reader.NodeType)); } } return; }
/// 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 }