: +91-265-2775555
 : +1-856-831-0505
 : +44-788-443-25-28
 : +45-4052-3137
Prakash software blogs - software development,custom sharepoint,MOSS 2007,Telerik development,C#.net,sharepoint webparts > Tag Clouds
How to Open Large (>2GB) XML files without Loading the xml Files

Problem: Open large xml files without loading the xml file (more than 2 GB)

Resolution: -

When we think of working with any XML file, we normally think of using XMLDocument (DOM) or DataSet.LoadXML(). Some programmers also use XPathDocument to load the xml file. When we use the above objects, we are loading the files into the system memory. The problem is that, if the size of the xml file is for e.g. 5 GB to 7 GB, we have to load the complete file in System’s Memory. This will cost us systems memory and will throw “System out of Memory Exception”.

The best approach to process such large files is to avoid loading the files into the memory. Microsoft has provided with XmlTextReader class.  XmlTextReader helps us to process the xml file line by line. In this way we are not loading the complete xml file into the memory but processing the file line by line, node by node. The line provides indicates the type, whether it is a StartElement, EndElement, etc… WE can get the complete child nodes and its collection for the parent node using oXmlTextReader.ReadOuterXML().

Below is an example of how to use XMLTextReader class: -

XmlTextReader AgencyTextReader = new XmlTextReader(filename);

AgencyTextReader.WhitespaceHandling = WhitespaceHandling.None;

 

while (AgencyTextReader.Read())

{

if (AgencyTextReader.NodeType == XmlNodeType.Element &&

AgencyTextReader.LocalName == "award" &&

AgencyTextReader.IsStartElement() == true)

            {

                  ProcessAwardNode(AgencyTextReader);

                  AgencyTextReader.Skip();

}

 }

private void ProcessAwardNode(XmlTextReader AwardReader)

{

XmlDocument AwardXmlDoc = new XmlDocument();

AwardXmlDoc.LoadXml(AwardReader.ReadOuterXml());

// we can use xpath as below

agencyID =AwardXmlDoc.SelectSingleNode("award/agencyID").InnerText;

}

Open native windows application from web page

Recently I needed to open windows calculator from web page. Different browser has different approach to allow this. Below I have mentioned how to implement this for Internet Explorer 6.0/7.0/8.0 and Mozilla Firefox 3.0/3.5.

Internet Explorer:

In IE you need to create the ActiveX control to open the application. You need to write the following HTML code to do this.

<html>

<head>

</head>

<body>

    <input type="button" id ="buttonOpenCalculator"  value="Open Calculator" />

    <script type="text/javascript">

      function OpenCalculator()

      {

            var commandShell = new ActiveXObject("WScript.Shell");

            commandShell.Run("calc");

            commandShell.Quit;

      }

      document.getElementById("buttonOpenCalculator").attachEvent('onclick', OpenCalculator);

    </script>

</body>

</html>

Here when you click the button “Open Calculater” calculator will be opened.

Note: In order to open calculator you need change browser settings as below.

·         Go to Tools > Internet Options > Security Tab > Local Intranet.

·         Click Custom Level in Security level for this zone.

·         SetInitialize and script ActiveX controls not marked as safe” to prompt in ActiveX Controls and Plug-Ins zone.


 

Mozilla Firefox:

To open the application in web page from Mozilla you need to create an extension (.xpi).

In extension you need to write the following code in JS file.

 

//This is the function that will create a process for an application

eventListener = function(event) {

    // get system32 path for user;

    var userSystemFilePath = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("SysD", Components.interfaces.nsIFile);

    var stringApplicationPath = userSystemFilePath.path + "\\calc.exe";// "c:\\windows\\system32\\calc.exe";

    event.target.setAttribute("IsAddOnInstalled", "true");   

    var stringParameter = "";

    try

    {

        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

        var objectFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);

        objectFile.initWithPath(stringApplicationPath);

        if (objectFile.exists())

        {

            // create an nsIProcess

            var objectProcess = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);

            objectProcess.init(objectFile);

            // Run the process.

            // If first param is true, calling process will be blocked until

            // called process terminates.

            // Second and third params are used to pass command-line arguments

            // to the process.

            var arguments = null;

            var argumentLenth = 0;

            if (stringParameter)

            {

                arguments = [stringParameter];

                argumentLenth = 1;

            }

            objectProcess.run(false, arguments, argumentLenth);

        }

    }

    catch (error)

    {

        alert(error );

    }

}

document.addEventListener("sendEvent", eventListener, false, true);

 

 

 

In the web page you need to write the following code

 

<html>

<head>

</head>

<body>

    <input type="button" id ="buttonOpenCalculator"  value="Open Calculator" />

    <script type="text/javascript">

        var element = document.createElement("ExtensionDataElement");

        element.setAttribute("IsAddOnInstalled", 'false');

        document.documentElement.appendChild(element);

       

        function OpenCalculator()

        {

            var event = document.createEvent("Events");

            event.initEvent("sendEvent", true, false);

            element.dispatchEvent(event);

            if (element.getAttribute("IsAddOnInstalled") == 'false')

            {

                window.open("download url of xpi","calculator","","");

            }

        }

        document.getElementById("buttonOpenCalculator").addEventListener('click', OpenCalculator, false);

    </script>

</body>

</html>

 

If you run the code and click the button for first time you will be prompted to install xpi file. Firefox needs to be restarted after installing the extension. Clicking the button now will open the calculator.

A for Ajax, Part 1: Expectations from Ajax enabled web applications
Many of today’s web applications are Ajax enabled. What are our expectations from an Ajax enabled web application vis-à-vis traditional web applications?

Traditional page-based web applications process every request entirely on the server and sends across a HTML response that is rendered on the browser of the client machine. The HTML will contain the content [the HTML markup] and data. Refer Figure 1.
 Traditional Processing
Figure 1: The traditional request-response web application architecture

This request-response strategy is characterized by:
• high end server hardware.
• a browser on the client machine acting as a dumb terminal.
• complicated state management.
• the user waiting for a response after every request because of the synchronous nature of the request-response operation.

Considering that client machines are quite powerful nowadays, it makes sense to move part of the processing from the server to the client. This is the strategy followed in Ajax enabled web applications. The immediate gain is that this frees server resources, allowing the same server hardware to service more client requests concurrently. Ajax enabled web applications scale better than the traditional applications.

In an Ajax enabled web application, the only time the server processes a request entirely on the server is when the page is loaded the first time. This is the only time that the server serves content and maybe some data. For subsequent requests it serves only raw data, encoded in a given format. The client receives data feeds and updates only the parts of the Web page that have changed using JavaScript. In Ajax enabled web applications, UI updates take effect without postback. Refer Figure 2.
 
Ajax Processing
Figure 2: The Ajax enabled web application architecture

Since only raw data is served, the size of the data is much smaller than what would be served by a traditional web application. This speeds up individual request and response, resulting in reduced network traffic and bandwidth requirements. Ajax enabled web applications are faster than the traditional applications.

By default the request-response strategy of an Ajax enabled web application is asynchronous in nature. This means that the user can continue to use the application while the client requests information from the server in the background and updates the UI [think Google Map]. Ajax enabled web applications are more responsive than the traditional applications.

In Ajax enabled web applications UI processing happens at the client. This makes it possible to achieve rich functionalities such as drag and drop, auto-completion, smooth animation, client side calculation, validation, etc. JavaScript libraries, such as ExtJS, bring in more sophistication to the UI. Ajax enabled web applications result in a rich user experience.

In summary, Ajax enabled web applications [with respect to traditional web applications] can be expected to:
1. scale better
2. have better overall performance
3. result in a richer user experience
Create Share folder on remote machine
When you try to give share permission to folder on remote machine (i.e from your pc to //prakash-new/c$/test2) you can not do manually. Windows does not give you a Tab of Sharing in Folder properties.
 
You could achive that by below code.
 

using System.Management;

// ManagementClass managementClass = new ManagementClass("Win32_Share"); // For local PC

// replace "ssc"  to your remote server

ManagementClass managementClass = new ManagementClass("\\\\ssc\\root\\cimv2", "Win32_Share", null);

// Create ManagementBaseObjects for in and out parameters

ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");

ManagementBaseObject outParams;

// Set the input parameters

inParams["Description"] = "Bhargav";

inParams["Name"] = "Bhargav share";

inParams["Path"] = @"c:\Test2";//@"D:\Test2\Test3";

// in above path params "c:\Test2" will take From SSC

inParams["Type"] = 0x0; // Disk Drive

//Another Type:

// DISK_DRIVE = 0x0

// PRINT_QUEUE = 0x1

// DEVICE = 0x2

// IPC = 0x3

// DISK_DRIVE_ADMIN = 0x80000000

// PRINT_QUEUE_ADMIN = 0x80000001

// DEVICE_ADMIN = 0x80000002

// IPC_ADMIN = 0x8000003

//inParams["MaximumAllowed"] = int maxConnectionsNum;

// Invoke the method on the ManagementClass object

outParams = managementClass.InvokeMethod("Create", inParams, null);

// Check to see if the method invocation was successful

if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)

{

throw new Exception("Unable to share directory.");

}

Hope this will be of some help to you in your project.