Musings

A random collection

Archive for April 2011

C#: XML Processing

  1. LINQ to XML way
    System.Xml.Linq.XElement booksFromFile = System.Xml.Linq.XElement.Load(@"books.xml");
    Console.WriteLine(booksFromFile);
    
  2. Use XmlTextReader class
    XmlTextReader reader = new XmlTextReader ("books.xml");
    while (reader.Read()) 
    {
        switch (reader.NodeType) 
        {
        case XmlNodeType.Element: // The node is an element.
            Console.Write("<" + reader.Name);
            Console.WriteLine(">");
            break;
        case XmlNodeType.Text: //Display the text in each element.
            Console.WriteLine (reader.Value);
            break;
        case XmlNodeType.EndElement: //Display the end of the element.
            Console.Write("</" + reader.Name);
            Console.WriteLine(">");
            break;
        }
    }
    Console.ReadLine();
    
  3. private System.Windows.Forms.Button btnProcessXML;
    private System.Windows.Forms.RichTextBox rtbResultString;
    private string strReturnText = string.Empty;
    private void btnProcessXML_Click(object sender, System.EventArgs e)
    {
        XmlTextReader reader = new XmlTextReader("FieldNames.xml");
    
        reader.WhitespaceHandling = WhitespaceHandling.All;
        XmlDocument xmlDoc = new XmlDocument();
    
        // Load the file into the XmlDocument
        xmlDoc.Load(reader);
    
        // Close off the connection to the file.
        reader.Close();
    
        // Find the root node,
        XmlNode xnod = xmlDoc.DocumentElement;
        GetFormattedFieldString(xnod);
        rtbResultString.Text = strReturnText;
    }
    
    # region GetFormattedFieldString Method
    ///
    /// Constructs the 2D string array from the field text and length from XML.
    ///
    /// XmlNode
    private void GetFormattedFieldString(XmlNode xnod)
    {
        XmlNode fieldText, fieldLength;
        string strFieldText = string.Empty;
        // For an element node,
        if (xnod.NodeType == XmlNodeType.Element)
        {
            // retrive the childnodes.
            XmlNodeList mapChildNodes = xnod.ChildNodes;
            foreach(XmlNode xnodChildNode in mapChildNodes)
            {
                // Retrieve field text attribute.
                fieldText = xnodChildNode.SelectSingleNode("@text");
                // Retrieve field length attribute.
                fieldLength = xnodChildNode.SelectSingleNode("@length");
                strReturnText = strReturnText + fieldText.Value + " : " + fieldLength.Value + "\n";
                // strReturnText = strReturnText + GetFieldText(fieldText.Value, fieldLength.Value) + "\n";
                // If has subfields, call the method recursively.
                if (xnodChildNode.HasChildNodes )
                {
                    GetFormattedFieldString(xnodChildNode);
                }
            }
        }
    }
    # endregion
    
Advertisements

Written by curious

April 27, 2011 at 1:15 pm

Posted in programming

TECH: Vim Folding

My ~/_vimrc file

set expandtab
set ts=4
set sw=4
set textwidth=80
set ai

au BufWinLeave * silent! mkview
au BufWinEnter * silent! loadview

set nobackup
set nowritebackup
set directory=c:\temp\junk
set viewdir=c:\temp\junk
  1. No tabs, just spaces
    set ts=4
    set sw=4
    set expandtab
    
  2. No backups, clean up swaps
    set nobackup
    set nowritebackup
    set directory=c:\temp
    
  3. Folding Commands
    zfNj creates a fold from the cursor down N lines
    zf/string creates a fold from the cursor to string
    za toggle fold at the cursor
    zj moves the cursor to the next fold
    zk moves the cursor to the previous fold
    zo opens a fold at the cursor
    zO opens all folds at the cursor
    zm increases the foldlevel by one
    zM closes all open folds
    zr decreases the foldlevel by one
    zR decreases the foldlevel to zero -- all folds will be open
    zd deletes the fold at the cursor
    zE deletes all folds
    [z move to start of open fold
    ]z move to end of open fold
    
  4. Automatically save folds. Put the following lines in your $HOME/.vimrc
    set viewdir=c:\temp
    
    au BufWinLeave * mkview
    au BufWinEnter * silent loadview
    
    or
    au BufWinLeave *notes.txt silent! mkview
    au BufWinEnter *notes.txt silent! loadview
    
  5. Running programs without having to press enter
    :silent !pdflatex -quiet %
    

    or better run it asynchronously

    :!start pdflatex -quiet %
    

Written by curious

April 27, 2011 at 9:10 am

Posted in linux

LINUX: Squid Logs

How can I print squid logs with nicer time stamps?

tail -f /var/log/squid/access.log|perl -p -e 's/^([0-9]*)/"[".localtime($1)."]"/e'

Written by curious

April 26, 2011 at 10:37 am

Posted in linux

INT: Probability Questions

  1. Consider there are cereal boxes of a top brand containing one of four different types of toys with equal probability. What is the expected value of the number of cereal boxes which a person will need to buy in order to complete the collection of 4 toys.

Written by curious

April 7, 2011 at 7:31 pm

Posted in tech-tips