Example #1
0
  public static void Main() {
  
    XmlTextReader reader = new XmlTextReader(filename);
    reader.WhitespaceHandling=WhitespaceHandling.None;

    // Read the first part of the XML document
    while(reader.Read()) {
      // Display the elements and stop reading on the book endelement tag
      // then go to ReadPart2 to start another reader to read the rest of the file. 
      switch(reader.NodeType) {
       case XmlNodeType.Element:
        Console.WriteLine("Name: {0}", reader.Name);
        break;
       case XmlNodeType.Text:
        Console.WriteLine("  Element Text: {0}", reader.Value);
        break;
       case XmlNodeType.EndElement:
        // Stop reading when the reader gets to the end element of the book node.
        if ("book"==reader.LocalName) {
          Console.WriteLine("End reading first book...");
          Console.WriteLine();      
          goto ReadPart2;
        }
        break;
      } 
    } 

    // Read the rest of the XML document
    ReadPart2:
    Console.WriteLine("Begin reading second book...");

    // Create a new reader to read the rest of the document.
    XmlTextReader reader2 = new XmlTextReader(reader.GetRemainder());

    while(reader2.Read()) {
      switch (reader2.NodeType) {
        case XmlNodeType.Element:
         Console.WriteLine("Name: {0}", reader2.Name);
         break;
        case XmlNodeType.Text:
         Console.WriteLine("  Element Text: {0}", reader2.Value);
         break;
        case XmlNodeType.EndElement:
         // Stop reading when the reader gets to the end element of the book node.
         if ("book"==reader2.LocalName) {
           Console.WriteLine("End reading second book...");
           goto Done;
         }
         break;
      }
    }

    Done:
    Console.WriteLine("Done.");
    reader.Close(); 
    reader2.Close();
  }
        // ----------------------------------------------------

        // method to return an XML document (as a String) containing
        // a full list of supplier details from database, and using
        // a SQL Server XML technology (FOR XML) query
        public String GetSuppliersSqlXml()
        {
            // declare a String containing the SQL-XML stored proc to execute
            String strQuery = "GetSupplierXml";

            // create a new Connection object using connection string
            SqlConnection objConnect = new SqlConnection(m_ConnectString);

            // create new Command using stored proc name and Connection
            SqlCommand objCommand = new SqlCommand(strQuery, objConnect);

            // create a variable to hold an XmlTextReader object
            XmlTextReader objReader = null;

            String strXml = String.Empty;
            Char   QUOT   = (char)34;

            try
            {
                // open connection to the database
                objConnect.Open();

                // execute the stored proc to initialize the XmlTextReader
                objReader = (XmlTextReader)objCommand.ExecuteXmlReader();

                // create the document prolog
                DateTime datToday = DateTime.Now;
                strXml = "<?xml version=" + QUOT + "1.0" + QUOT + "?>"
                         + "<!-- Created: " + datToday.ToString("d") + " -->"
                         + "<SupplierList>";

                // read the first result row and ) read remainder
                objReader.ReadString();
                strXml += objReader.GetRemainder().ReadToEnd();

                // add the document epilog
                strXml += "</SupplierList>";
            }
            catch (Exception objErr)
            {
                throw (objErr);
            }
            finally
            {
                // close connection and destroy reader object
                objConnect.Close();
                objReader = null;
            }
            // return the XML document object to the calling routine
            return(strXml);
        }
Example #3
0
    // Test the GetRemainder method.
    public void TestXmlTextReaderGetRemainder()
    {
        Reset(3);
        xr.WhitespaceHandling = WhitespaceHandling.None;
        AssertEquals("GetRemainder (1)", true, xr.Read());
        AssertEquals("GetRemainder (2)", true, xr.Read());
        AssertEquals("GetRemainder (3)", true, xr.Read());
        AssertEquals("GetRemainder (4)", "book", xr.Name);
        AssertEquals("GetRemainder (5)", true, xr.Read());
        AssertEquals("GetRemainder (6)", "title", xr.Name);
        AssertEquals("GetRemainder (7)", true, xr.Read());
        AssertEquals("GetRemainder (8)", "Understanding The Linux Kernel", xr.Value);
        AssertEquals("GetRemainder (9)", true, xr.Read());
        AssertEquals("GetRemainder (10)", "title", xr.Name);
        AssertEquals("GetRemainder (11)", true, xr.Read());
        AssertEquals("GetRemainder (12)", "author", xr.Name);
        AssertEquals("GetRemainder (13)", true, xr.Read());
        AssertEquals("GetRemainder (14)", "Daniel P. Bovet and Marco Cesati", xr.Value);
        AssertEquals("GetRemainder (15)", true, xr.Read());
        AssertEquals("GetRemainder (16)", "author", xr.Name);
        AssertEquals("GetRemainder (17)", true, xr.Read());
        AssertEquals("GetRemainder (18)", "book", xr.Name);

        Reset(xr.GetRemainder());
        xr.WhitespaceHandling = WhitespaceHandling.None;
        AssertEquals("GetRemainder (19)", true, xr.Read());
        AssertEquals("GetRemainder (20)", "book", xr.Name);
        AssertEquals("GetRemainder (21)", true, xr.Read());
        AssertEquals("GetRemainder (22)", "title", xr.Name);
        AssertEquals("GetRemainder (23)", true, xr.Read());
        AssertEquals("GetRemainder (24)", "Learning Perl", xr.Value);
        AssertEquals("GetRemainder (25)", true, xr.Read());
        AssertEquals("GetRemainder (26)", "title", xr.Name);
        AssertEquals("GetRemainder (27)", true, xr.Read());
        AssertEquals("GetRemainder (28)", "author", xr.Name);
        AssertEquals("GetRemainder (29)", true, xr.Read());
        AssertEquals("GetRemainder (30)", "Randal L. Schwartz and Tom Christiansen", xr.Value);
        AssertEquals("GetRemainder (31)", true, xr.Read());
        AssertEquals("GetRemainder (32)", "author", xr.Name);
        AssertEquals("GetRemainder (33)", true, xr.Read());
        AssertEquals("GetRemainder (34)", "book", xr.Name);

        Clear();
    }
Example #4
0
        public void ParseFile()
        {
            XmlTextReader txtreader = null;
            XmlReader     reader = null;
            IEnumerator   el, ef, ee = null;

            try
            {
                // Load the reader with the data file and ignore all whitespace nodes.
                txtreader = new XmlTextReader(UserConfigFile.FullName);
                txtreader.WhitespaceHandling = WhitespaceHandling.None;
                reader = XmlReader.Create(txtreader.GetRemainder());

                _doc.Load(reader);

                XmlNode          root = _doc.DocumentElement;
                XmlNode          lang;
                XmlNode          ftype;
                XmlNode          ext;
                XmlNode          a, a2, au, al;
                Hashtable        ftypes;
                Hashtable        exts;
                string           sLang, sFiletype;
                MetricCollection metrics = KrakatauSettings.Settings.Metrics;

                el = root.ChildNodes.GetEnumerator();
                while (el.MoveNext())
                {
                    lang = (XmlNode)el.Current;
                    if (lang.Name.Equals("lang"))
                    {
                        a = lang.Attributes.GetNamedItem("name");
                        if (a != null && !"".Equals(a.Value))
                        {
                            sLang  = a.Value;
                            ftypes = new Hashtable();
                            _config.Add(sLang, ftypes);

                            ef = lang.ChildNodes.GetEnumerator();
                            while (ef.MoveNext())
                            {
                                ftype = (XmlNode)ef.Current;
                                if (ftype.Name.Equals("filetype"))
                                {
                                    a = ftype.Attributes.GetNamedItem("name");
                                    if (a != null && !"".Equals(a.Value))
                                    {
                                        sFiletype = a.Value;
                                        exts      = new Hashtable();
                                        ftypes.Add(sFiletype, exts);

                                        ee = ftype.ChildNodes.GetEnumerator();
                                        while (ee.MoveNext())
                                        {
                                            ext = (XmlNode)ee.Current;
                                            a   = ext.Attributes.GetNamedItem("name");
                                            a2  = ext.Attributes.GetNamedItem("value");

                                            if (a != null && !"".Equals(a.Value) && a2 != null && !"".Equals(a2.Value))
                                            {
                                                Ext e = new Ext(a.Value, a2.Value, sFiletype, sLang);
                                                exts.Add(e.Extension, e);
                                                _list.Add(e.ToString(), e);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (lang.Name.Equals("sets"))
                    {
                        ef = lang.ChildNodes.GetEnumerator();
                        while (ef.MoveNext())
                        {
                            ftype = (XmlNode)ef.Current;
                            if (ftype.Name.Equals("set"))
                            {
                                a = ftype.Attributes.GetNamedItem("name");
                                if (a != null && !"".Equals(a.Value))
                                {
                                    MetricSet ms = new MetricSet(a.Value);
                                    _sets.Add(ms.Name, ms);
                                    ee = ftype.ChildNodes.GetEnumerator();
                                    while (ee.MoveNext())
                                    {
                                        ext = (XmlNode)ee.Current;
                                        a   = ext.Attributes.GetNamedItem("id");
                                        au  = ext.Attributes.GetNamedItem("upper");
                                        al  = ext.Attributes.GetNamedItem("lower");

                                        if (a != null && !"".Equals(a.Value))
                                        {
                                            MetricDef md = new MetricDef(metrics[long.Parse(a.Value)]);
                                            if (au != null && !"".Equals(au.Value))
                                            {
                                                md.Upper(long.Parse(au.Value));
                                            }
                                            if (al != null && !"".Equals(al.Value))
                                            {
                                                md.Lower(long.Parse(al.Value));
                                            }
                                            ms.Add(md);
                                        }
                                    }
                                }
                            }
                        }
                        // Now remove the Metric Sets as they will be rebuilt during application execution.
                        root.RemoveChild(lang);
                    }
                }
            }

            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
Example #5
0
        public void ReadOptions()
        {
            if (!OptionsFile.Exists)
            {
                return;
            }

            XmlTextReader txtreader = null;
            XmlReader     reader = null;
            IEnumerator   el, ef = null;

            // Load the reader with the data file and ignore all whitespace nodes.
            txtreader = new XmlTextReader(OptionsFile.FullName);
            txtreader.WhitespaceHandling = WhitespaceHandling.None;

            // Implement the validating reader over the text reader.
            reader = XmlReader.Create(txtreader.GetRemainder());

            _doc.Load(reader);

            XmlNode root = _doc.DocumentElement;
            XmlNode child;
            XmlNode extNode;
            XmlNode a;

            Extensions.Clear();
            ExcludedDirectories.Clear();

            if (((a = root.Attributes.GetNamedItem("projectFile")) != null) && !"".Equals(a.Value))
            {
                _project.ProjectFile = new FileInfo(a.Value);
            }

            el = root.ChildNodes.GetEnumerator();
            while (el.MoveNext())
            {
                child = (XmlNode)el.Current;

                if (child.Name.Equals("include"))
                {
                    ef = child.ChildNodes.GetEnumerator();
                    while (ef.MoveNext())
                    {
                        extNode = (XmlNode)ef.Current;
                        if (extNode.Name.Equals("ext"))
                        {
                            if (((a = extNode.Attributes.GetNamedItem("value")) != null) && !"".Equals(a.Value))
                            {
                                Extensions.Add(new Ext(a.Value, "*." + a.Value, "", ""));
                            }
                        }
                    }
                }
                else if (child.Name.Equals("exclude"))
                {
                    ef = child.ChildNodes.GetEnumerator();
                    while (ef.MoveNext())
                    {
                        extNode = (XmlNode)ef.Current;
                        if (extNode.Name.Equals("dir"))
                        {
                            if (((a = extNode.Attributes.GetNamedItem("value")) != null) && !"".Equals(a.Value))
                            {
                                ExcludedDirectories.Add(new DirectoryInfo(a.Value));
                            }
                        }
                    }
                }
            }
            if (reader != null)
            {
                reader.Close();
            }
            if (txtreader != null)
            {
                txtreader.Close();
            }
        }