3 using System.Reflection;
5 using System.Xml.Schema;
7 using BF = System.Reflection.BindingFlags;
11 public static void Main (string [] args)
15 } catch (Exception ex) {
16 Console.WriteLine ("ERROR: " + ex.Message);
20 static void Run (string [] args)
22 if (args.Length < 1) {
23 Console.WriteLine ("USAGE: mono dtd2xsd.exe instance-xmlfile [output-xsdfile]");
26 XmlTextReader xtr = new XmlTextReader (args [0]);
27 XmlSchema xsd = Dtd2Xsd.Run (xtr);
29 xsd.Write (new StreamWriter (args [1]));
31 xsd.Write (Console.Out);
37 public static XmlSchema Run (XmlTextReader xtr)
39 while (xtr.NodeType != XmlNodeType.DocumentType) {
41 throw new Exception ("DTD did not appeared.");
44 // Hacky reflection part
46 BF flag = BF.NonPublic | BF.Instance;
48 // In Mono NET_2_0 XmlTextReader is just a wrapper which
49 // does not contain DTD directly.
50 FieldInfo fi = typeof (XmlTextReader).GetField ("source", flag);
52 impl = fi.GetValue (xtr);
54 PropertyInfo pi = impl.GetType ().GetProperty ("DTD", flag);
55 object dtd = pi.GetValue (impl, null);
57 dtd.GetType ().GetMethod ("CreateXsdSchema", flag);
58 object o = mi.Invoke (dtd, null);