Wednesday, September 29, 2010

How to: Merge Multiple Xmls?

Easiest, simplest, fastest way? ...could be, well, if you don't want any checks to be performed(duplicates, zombies, etc), and are using .NET 3.5 or above:

var ResultXml = XDocument.Load("file1.xml");
ResultXml.Root.Add(XDocument.Load("file2.xml").Root.Elements());

Cool, right? (0:
Or if you are "still" a fan of pre-date'rs, use XmlDocument:

static void AppendChildren(XmlWriter xmlWriter, string strFilePath)
{
    using (XmlReader xmlReader = XmlReader.Create(strFilePath))
    {
        xmlReader.MoveToContent();
        int nDepth = xmlReader.Depth + 1;
        if (xmlReader.Read())
        {
            while (xmlReader.Depth == nDepth)
            {
                xmlWriter.WriteNode(xmlReader, true);
            }
        }
    }
}

Or better, if you want, would be to make it an extention method:

static void AppendChildren(this XmlWriter xmlWriter, string strFilePath)

All you have to do is to call .ToAppendChildren() method.

Happy programming! (0:

Friday, September 24, 2010

.NET: is vs as

Small difference between is and as

From MSDN: is keyword
The is operator is used to check whether the run-time type of an object is compatible with a given type

From MSDN: as keyword
The as operator is used to perform conversions between compatible types.

Example:

object s = "this is a test";
string str=string.Empty;
if( s is string)
{    str = s as string;
if(!string.IsNullOrEmpty(str))
 DoSomething();
}

Note that, the example above is an anti-pattern. It's expensive to do is then as. Why? because it unboxes 's' object twice. For reference types, you should just do as, then check for null to see if it worked.

Like following:

string str = s as string;
if(s!=null)
{
 DoSomething();
}

Happy programming!

Wednesday, September 22, 2010

Kanban vs Scrum

Kanban vs Scrum: An interesting article that I came across, thought I'd keep a link.

...Kanban is not a project management or software development lifecycle method. It is an approach to change management - a framework for catalyzing change in an organization


...Scrum uses commitment as its control mechanism for provoking changes. Commitment exists at two levels: at the personal daily level - this is reinforced with the Scrum and the 3 questions “what did you do for us yesterday?” “what will you do for us today?” and “Is anything impeding you from meeting your commitment today?”

Tuesday, September 7, 2010

Simplest console based client/server program

For those who want a quick and easy way to grasp the client/server model.

class Program
{
    static void Main(string[] args)
    {
        bool sender = false;
        if (args.Length > 0) sender = true;
        if (sender)
        {
            try
            {
                Console.WriteLine("..:: Client ::..");
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000);
                sock.Connect(ipe);
                while (true)
                {
                    string toSend = Console.ReadLine();
                    sock.Send(Encoding.UTF32.GetBytes(toSend));
                    Console.WriteLine("Sent.");
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
        else
        {
            try
            {
                Console.WriteLine("..:: Server ::..");
                IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 5000);
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

                sock.Bind(ipe);
                sock.Listen(4);
                int nCount = 0;
                while (true)
                {
                    nCount++;
                    Socket newSock = sock.Accept();
                    if (!newSock.Connected) continue;
                    else
                    {
                        byte[] buffer = new byte[1024];//Can pull from a central config file
                        if (newSock.Receive(buffer) > 0) { Console.WriteLine("Received:"); Console.WriteLine(Encoding.UTF32.GetString(buffer)); }
                        newSock.Close();
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
}

Usually, almost always, you send the number of bytes before sending the actual text, so that the other side would know how much data to expect.

Happy coding!

Related Posts

Popular Posts