10 Aralık 2015 Perşembe

C# LinQ : XML Write and Read

XML File:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LumiaMobiles>  
  3.   <LumiaMobile>  
  4.     <ModelId>lumia550</ModelId>  
  5.     <Height>136.1 mm</Height>  
  6.     <Width>67.8 mm</Width>  
  7.     <Thickness>9.9 mm</Thickness>  
  8.     <Weight>141.9 g</Weight>  
  9.   </LumiaMobile>  
  10.   <LumiaMobile>  
  11.     <ModelId>lumia950</ModelId>  
  12.     <Height>145 mm</Height>  
  13.     <Width>73.2 mm</Width>  
  14.     <Thickness>8.2 mm</Thickness>  
  15.     <Weight>150 g</Weight>  
  16.   </LumiaMobile>  
  17. </LumiaMobiles>  
Corresponding class for XML Data:
  1. public class LumiaMobile  
  2. {  
  3.    public string ModelId { getset; }  
  4.    public string Height { getset; }  
  5.    public string Width { getset; }  
  6.    public string Thickness { getset; }  
  7.    public string Weight { getset; }  
  8. }  
Code to Write XML File 
  1. private void WriteLumiaMobilesDetails(List<LumiaMobile> lstLumiaMobiles)  
  2. {  
  3.     using (XmlWriter writer = XmlWriter.Create("LumiaMobiles2.xml"))  
  4.     {  
  5.         writer.WriteStartDocument();  
  6.         writer.WriteStartElement("LumiaMobiles");  
  7.         foreach (var mobile in lstLumiaMobiles)  
  8.         {  
  9.             writer.WriteStartElement("LumiaMobile");  
  10.             writer.WriteElementString("ModelId", mobile.ModelId);  
  11.             writer.WriteElementString("Height", mobile.Height);  
  12.             writer.WriteElementString("Width", mobile.Width);  
  13.             writer.WriteElementString("Thickness", mobile.Thickness);  
  14.             writer.WriteElementString("Weight", mobile.Weight);  
  15.             writer.WriteEndElement();  
  16.         }  
  17.   
  18.         writer.WriteEndElement();  
  19.         writer.WriteEndDocument();  
  20.     }  
  21. }  
Calling Write XML Data Method: 
  1. static void Main(string[] args)  
  2. {  
  3.     Program p = new Program();              
  4.     List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();  
  5.     lstLumiaMobiles.Add(new LumiaMobile  
  6.     {  
  7.         ModelId = "lumia550",  
  8.         Height = "136.1 mm",  
  9.         Width = "67.8 mm",  
  10.         Thickness = "9.9 mm",  
  11.         Weight = "141.9 g"  
  12.     });  
  13.   
  14.     lstLumiaMobiles.Add(new LumiaMobile  
  15.     {  
  16.         ModelId = "lumia950",  
  17.         Height = "145 mm",  
  18.         Width = "73.2 mm",  
  19.         Thickness = "8.2 mm",  
  20.         Weight = "150 g"  
  21.     });  
  22.     p.WriteLumiaMobilesDetails(lstLumiaMobiles);              
  23. }  
Code to Read XML Data:
  1. public List<LumiaMobile> ReadLumiaMobilesDetails()  
  2. {  
  3.     List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();  
  4.     try  
  5.     {  
  6.         using (XmlReader xr = XmlReader.Create(@"LumiaMobiles.xml"))  
  7.         {  
  8.             XDocument xdoc = XDocument.Load(xr);  
  9.             var LumiaMobiles = xdoc.Descendants("LumiaMobiles").Descendants("LumiaMobile");  
  10.             foreach (var mobile in LumiaMobiles)  
  11.             {  
  12.                 lstLumiaMobiles.Add(new LumiaMobile  
  13.                 {  
  14.                     ModelId = Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value),  
  15.                     Height = Convert.ToString(mobile.Descendants("Height").FirstOrDefault().Value),  
  16.                     Width = Convert.ToString(mobile.Descendants("Width").FirstOrDefault().Value),  
  17.                     Thickness = Convert.ToString(mobile.Descendants("Thickness").FirstOrDefault().Value),  
  18.                     Weight = Convert.ToString(mobile.Descendants("Weight").FirstOrDefault().Value)  
  19.                 });  
  20.             }  
  21.         }  
  22.     }  
  23.     catch (Exception)  
  24.     {  
  25.   
  26.     }  
  27.     return lstLumiaMobiles;  
  28. }  
Calling Read XML Data Method: 
  1. static void Main(string[] args)  
  2. {  
  3.    Program p = new Program();  
  4.    p.ReadLumiaMobilesDetails();   
  5. }  
Code to Update XML Data:
  1. private void UpdateXmlElementforMobile(string ModelId, string element, string elementValue)  
  2. {  
  3.   
  4.     XDocument xdoc = XDocument.Load("LumiaMobiles.xml");  
  5.     var LumiaMobiles = xdoc.Descendants("LumiaMobile");  
  6.     foreach (var mobile in LumiaMobiles)  
  7.     {  
  8.         if (mobile.Descendants("ModelId").Any())  
  9.         {  
  10.             if (Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value) == ModelId)  
  11.             {  
  12.                 mobile.Descendants(element).FirstOrDefault().Value = elementValue;  
  13.                 xdoc.Save("LumiaMobiles.xml");  
  14.             }  
  15.         }  
  16.     }  
  17. }  
Calling Update XML Data Method: 
  1. static void Main(string[] args)  
  2. {  
  3.    Program p = new Program();  
  4.    p.UpdateXmlElementforMobile("lumia950""Height""145.0 mm");  
  5. }  
Complete Code is:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Xml;  
  5. using System.Xml.Linq;  
  6.   
  7. namespace XMLtoLINQ  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Program p = new Program();  
  14.             p.ReadLumiaMobilesDetails();  
  15.             List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();  
  16.             lstLumiaMobiles.Add(new LumiaMobile  
  17.             {  
  18.                 ModelId = "lumia550",  
  19.                 Height = "136.1 mm",  
  20.                 Width = "67.8 mm",  
  21.                 Thickness = "9.9 mm",  
  22.                 Weight = "141.9 g"  
  23.             });  
  24.   
  25.             lstLumiaMobiles.Add(new LumiaMobile  
  26.             {  
  27.                 ModelId = "lumia950",  
  28.                 Height = "145 mm",  
  29.                 Width = "73.2 mm",  
  30.                 Thickness = "8.2 mm",  
  31.                 Weight = "150 g"  
  32.             });  
  33.   
  34.             p.WriteLumiaMobilesDetails(lstLumiaMobiles);  
  35.   
  36.             p.UpdateXmlElementforMobile("lumia950""Height""145.0 mm");  
  37.         }  
  38.   
  39.         private void UpdateXmlElementforMobile(string ModelId, string element, string elementValue)  
  40.         {  
  41.   
  42.             XDocument xdoc = XDocument.Load("LumiaMobiles.xml");  
  43.             var LumiaMobiles = xdoc.Descendants("LumiaMobile");  
  44.             foreach (var mobile in LumiaMobiles)  
  45.             {  
  46.                 if (mobile.Descendants("ModelId").Any())  
  47.                 {  
  48.                     if (Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value) == ModelId)  
  49.                     {  
  50.                         mobile.Descendants(element).FirstOrDefault().Value = elementValue;  
  51.                         xdoc.Save("LumiaMobiles.xml");  
  52.                     }  
  53.                 }  
  54.             }  
  55.         }  
  56.   
  57.         private void WriteLumiaMobilesDetails(List<LumiaMobile> lstLumiaMobiles)  
  58.         {  
  59.             using (XmlWriter writer = XmlWriter.Create("LumiaMobiles2.xml"))  
  60.             {  
  61.                 writer.WriteStartDocument();  
  62.                 writer.WriteStartElement("LumiaMobiles");  
  63.                 foreach (var mobile in lstLumiaMobiles)  
  64.                 {  
  65.                     writer.WriteStartElement("LumiaMobile");  
  66.                     writer.WriteElementString("ModelId", mobile.ModelId);  
  67.                     writer.WriteElementString("Height", mobile.Height);  
  68.                     writer.WriteElementString("Width", mobile.Width);  
  69.                     writer.WriteElementString("Thickness", mobile.Thickness);  
  70.                     writer.WriteElementString("Weight", mobile.Weight);  
  71.                     writer.WriteEndElement();  
  72.                 }  
  73.   
  74.                 writer.WriteEndElement();  
  75.                 writer.WriteEndDocument();  
  76.             }  
  77.         }  
  78.   
  79.         public List<LumiaMobile> ReadLumiaMobilesDetails()  
  80.         {  
  81.             List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();  
  82.             try  
  83.             {  
  84.                 using (XmlReader xr = XmlReader.Create(@"LumiaMobiles.xml"))  
  85.                 {  
  86.                     XDocument xdoc = XDocument.Load(xr);  
  87.                     var LumiaMobiles = xdoc.Descendants("LumiaMobiles").Descendants("LumiaMobile");  
  88.                     foreach (var mobile in LumiaMobiles)  
  89.                     {  
  90.                         lstLumiaMobiles.Add(new LumiaMobile  
  91.                         {  
  92.                             ModelId = Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value),  
  93.                             Height = Convert.ToString(mobile.Descendants("Height").FirstOrDefault().Value),  
  94.                             Width = Convert.ToString(mobile.Descendants("Width").FirstOrDefault().Value),  
  95.                             Thickness = Convert.ToString(mobile.Descendants("Thickness").FirstOrDefault().Value),  
  96.                             Weight = Convert.ToString(mobile.Descendants("Weight").FirstOrDefault().Value)  
  97.                         });  
  98.                     }  
  99.                 }  
  100.             }  
  101.             catch (Exception)  
  102.             {  
  103.   
  104.             }  
  105.             return lstLumiaMobiles;  
  106.         }  
  107.     }  
  108.   
  109.     public class LumiaMobile  
  110.     {  
  111.         public string ModelId { getset; }  
  112.         public string Height { getset; }  
  113.         public string Width { getset; }  
  114.         public string Thickness { getset; }  
  115.         public string Weight { getset; }  
  116.     }  
  117. }  

Div tag actions using JQuery.

  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  6.     <script>  
  7.     // Function which is called on div click  
  8.     function MyFunction()  
  9.     {  
  10.         alert("Div Clicked");  
  11.     };  
  12.     $(document).ready(function()  
  13.     {  
  14.         // Adding text to Div tag  
  15.         $('#MyDiv').text('Add to Favorites Conformation');  
  16.         // Changing div color  
  17.         $('#MyDiv').css("background-color""yellow");  
  18.         // Adding Click event to div tag  
  19.         $("#MyDiv").attr("onclick"'MyFunction()');  
  20.         // Empty the Div content  
  21.         $('#divContainer').empty();  
  22.         // Adding html code to div   
  23.         $("#myEmptyDiv").html("<h2>Ramakrishna Basagalla</h2>");  
  24.     });  
  25.     </script>  
  26. </head>  
  27.   
  28. <body>  
  29.     <div id="MyDiv">MyDivTag</div>  
  30.     <div id="divContainer">My Default text</div>  
  31.     <div id="myEmptyDiv"></div>  
  32. </body>  
  33.   
  34. </html>  

15 Ekim 2015 Perşembe

JavaScript Number değer kontrolü

function numara() { 
var str = $(\"#miktar\").val();
var sonuc; 
          
for (i = 0;i < str.length;i++) {
   a = str.substr(i, 1);
   if (a != '0' && a != '1' && a != '2' && a != '3' && a != '4' && a != '5' && a != '6' && a != '7' && a != '8' && a != '9') {
 alert( \"Miktar alanına sadece Number deger girilebilir.!\");
 break;
    }
  }
}



alternatif

  1. <HTML>  
  2.   
  3. <HEAD>  
  4.     <script type="text/Javascript"> function checkDec(el)  
  5.     {   
  6.       var ex = /^[0-9]+\.?[0-9]*$/;  
  7.       if(ex.test(el.value)==false)  
  8.       {  
  9.         el.value = el.value.substring(0,el.value.length - 1);  
  10.       }  
  11.     }   
  12.     </script>  
  13. </HEAD>  
  14.   
  15. <BODY>  
  16.     <input type="text" id="" onkeyup="checkDec(this);" />   
  17.  </BODY>  
  18.   
  19. </HTML> 

18 Eylül 2015 Cuma

Linux , IP port erişim test

Linux üzerinden herhangi bir ip ve port erişimin kontrol edilmesi için gerekli ola kod:

nc -vz IP_Address Port
nc -vz 203.0.113.96 21