Showing posts with label Quick Note. Show all posts
Showing posts with label Quick Note. Show all posts

Thursday, September 14, 2023

How to locate and replace special characters in an XML file with Visual C# .NET

We can use the SecurityElement.Escape method to replace the invalid XML characters in a string with their valid XML equivalent. The following table shows the invalid XML characters and their respective replacements

Character Name Entity Reference Character Reference Numeric Reference
Ampersand & & &
Left angle bracket < < <
Right angle bracket > > >
Straight quotation mark " " '
Apostrophe ' ' "

Sample Usage of this Escape method.

//Usage
srtXML = SecurityElement.Escape(strXML);
  

For this you need to import System.Security namespace. Alternatively you can also use this simple replace method with all special characters in a single method like below

public string EscapeXml(string s)
{
    string toxml = s;
    if (!string.IsNullOrEmpty(toxml))
    {
        // replace special chars with entities
        toxml = toxml.Replace("&", "&");
        toxml = toxml.Replace("'", "'");
        toxml = toxml.Replace("\"", """);
        toxml = toxml.Replace(">", ">");
        toxml = toxml.Replace("<", "&lt;");
    }
    return toxml;
}
  

Hope this is useful!

Monday, September 10, 2018

Git Cheat sheet for all

Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people. Git is one of the, if the not the, most popular version control systems available. Its used by millions of projects across all languages.

Remembering all those commands to perform common git tasks can be a bit of a nightmare for anyone.

Creating Repositories

# create new repository in current directory
git init

# clone a remote repository
git clone [url]
# for example cloning the entire jquery repo locally
git clone https://github.com/inagasai/nsmodel


Branches and Tags

# List all existing branches with the latest commit comment 
git branch –av

# Switch your HEAD to branch
git checkout [branch]

# Create a new branch based on your current HEAD
git branch [new-branch]

# Create a new tracking branch based on a remote branch
git checkout --track [remote/branch]
# for example track the remote branch named feature-branch-foo
git checkout --track origin/feature-branch-foo

# Delete a local branch
git branch -d [branch]

# Tag the current commit
git tag [tag-name]


Local Changes

# List all new or modified files - showing which are to staged to be commited and which are not 
git status

# View changes between staged files and unstaged changes in files
git diff

# View changes between staged files and the latest committed version
git diff --cached
# only one file add the file name
git diff --cached [file]

# Add all current changes to the next commit
git add [file]

# Remove a file from the next commit
git rm [file]

# Add some changes in < file> to the next commit
# Watch these video's for a demo of the power of git add -p - http://johnkary.net/blog/git-add-p-the-most-powerful-git-feature-youre-not-using-yet/
git add -p [file]

# Commit all local changes in tracked  files
git commit –a
git commit -am "An inline  commit message"

# Commit previously staged changes
git commit
git commit -m "An inline commit message"

# Unstages the file, but preserve its contents

git reset [file]


Commit History

# Show all commits, starting from the latest 
git log 

# Show changes over time for a specific file 
git log -p [file]

# Show who changed each line in a file, when it was changed and the commit id
git blame -c [file]


Update and Publish

# List all remotes 
git remote -v

# Add a new remote at [url] with the given local name
git remote add [localname] [url]

# Download all changes from a remote, but don‘t integrate into them locally
git fetch [remote]

# Download all remote changes and merge them locally
git pull [remote] [branch]

# Publish local changes to a remote 
git push [remote] [branch]

# Delete a branch on the remote 
git branch -dr [remote/branch]

# Publish your tags to a remote
git push --tags


Merge & Rebase

# Merge [branch] into your current HEAD 
git merge [branch]

# Rebase your current HEAD onto [branch]
git rebase [branch]

# Abort a rebase 
git rebase –abort

# Continue a rebase after resolving conflicts 
git rebase –continue

# Use your configured merge tool to solve conflicts 
git mergetool

# Use your editor to manually solve conflicts and (after resolving) mark as resolved 
git add <resolved- file>
git rm <resolved- file>


Undo

# Discard all local changes and start working on the current branch from the last commit
git reset --hard HEAD

# Discard local changes to a specific file 
git checkout HEAD [file]

# Revert a commit by making a new commit which reverses the given [commit]
git revert [commit]

# Reset your current branch to a previous commit and discard all changes since then 
git reset --hard [commit]

# Reset your current branch to a previous commit and preserve all changes as unstaged changes 
git reset [commit]

#  Reset your current branch to a previous commit and preserve staged local changes 
git reset --keep [commit]

Hope this helps for quick reference!!

Tuesday, August 07, 2018

Quick note: How to get backup history of a database?

Here is how you can get via SQL Query, This works on any SQL Server version

USE database_name
GO
-- Get Backup History for required database
SELECT TOP 100
s.database_name,
m.physical_device_name,
CAST(CAST(s.backup_size / 1000000 AS INT) AS VARCHAR(14)) + ' ' + 'MB' AS bkSize,
CAST(DATEDIFF(second, s.backup_start_date,
s.backup_finish_date) AS VARCHAR(4)) + ' ' + 'Seconds' TimeTaken,
s.backup_start_date,
CAST(s.first_lsn AS VARCHAR(50)) AS first_lsn,
CAST(s.last_lsn AS VARCHAR(50)) AS last_lsn,
CASE s.[type] WHEN 'D' THEN 'Full'
WHEN 'I' THEN 'Differential'
WHEN 'L' THEN 'Transaction Log'
END AS BackupType,
s.server_name,
s.recovery_model
FROM msdb.dbo.backupset s
INNER JOIN msdb.dbo.backupmediafamily m ON s.media_set_id = m.media_set_id
WHERE s.database_name = DB_NAME() -- Remove this line for all the database
ORDER BY backup_start_date DESC, backup_finish_date
GO

Hope this helps!

Wednesday, June 13, 2018

'TRUNCATE_ONLY' is not a recognized BACKUP option.

In Microsoft SQL server 2000 and 2005, as part of the ‘BACKUP LOG’ command, there was an option to truncate the log file, without storing a backup to file. In Microsoft SQL Server 2008, this option has been removed, and you can now no longer use the ‘TRUNCATE_ONLY’ option when performing a transaction log backup.

The truncate only option is used to truncate the log file. This is generally done so you can then shrink the transaction log file, and recover the disk space back to the file system.

I ran into this issue, when we are migrating our servers from 2005 to higher version.

Msg 155, Level 15, State 1, Line 1
'TRUNCATE_ONLY' is not a recognized BACKUP option.

To truncate the transaction log file in Microsoft SQL Server 2008 or 2012 and above, without making an actual transaction log backup (possibly due to free space limitations), you need to change the recovery model of the database to “Simple”, and then change it back to “Full” (or “Bulked Logged” if that’s what it was previously).

USE ps;
GO

-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE ps
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (ps_log, 1);  
-- here 2 is the file ID for trasaction log file
--you can also mention the log file name (dbname_log)
GO

-- Reset the database recovery model.
ALTER DATABASE ps
SET RECOVERY FULL;
GO

Hope this helps.

Friday, June 08, 2018

Quicknote: install stunnel as service

stunnel is an open-source multi-platform application used to provide universal TLS/SSL tunneling service. stunnel is used to provide secure encrypted connections for clients or servers that do not speak TLS or SSL natively.

Installing and setting up is pretty standard and configuration is derived form stunnel.config file, Here is how you can setup and configure stunnel in windows

For setting up stunnel service as automatic run this command from command prompt: “stunnel.exe -install  -quiet" and start it.

This will install stunnel as service, open the service and set service as automatic and start service.

stunnel

How to Configure Stunnel Windows

stunnel is an open-source multi-platform application used to provide universal TLS/SSL tunneling service. stunnel is used to provide secure encrypted connections for clients or servers that do not speak TLS or SSL natively.

stunnel can be used as TLS proxy, I have used when we have issues with TLS 1.0 with one of API End point.

Server administrators and home users alike feel the pressure to secure their Internet communications, but not every application supports using Transport Layer Security (TLS). Recently all OS and all major stopped TLS 1.0 support. So we used Stunel as an fall back approach to TLS 1.0.

Step 1
Install Stunnel. Download the Windows binary file from Stunnel.org. Double-click on the executable "stunnel-4.34-installer.exe" and accept the default values for installing Stunnel on your computer.

Step 2
Copy a valid SSL public certificate to the directory "C:\Program Files (x86)\stunnel." To make things more trouble-free, combine the public key and private key certificates into one .PEM file.

Step 3
Modify the Stunnel configuration file. Open the file "C:\Program Files (x86)\stunnel\stunnel.conf" using a text editor such as Notepad. Modify the file to include the line "cert = C:\Program Files (x86)\stunnel\," where is the name of your certificate file.

Here is the example of my config file

client = yes
[myService1]
accept          = 4010
connect         = www.pld.ups.com:443
sslVersion 	= TLSv1.2

[myService2]
accept          = 4011
connect         = wwwcie.ups.com:443
sslVersion 	= TLSv1.2

Step 4
Configure Stunnel to start automatically. Navigate to the Start menu, "stunnel" folder and click on "Service Install." Stunnel will configure a Windows service called "stunnel" to start automatically when the computer boots.

Monday, June 04, 2018

Quick note: What does spherical, cylindrical, and axis mean in an eyeglass prescription?

  • Spherical (or sphere): Specifies if you’re have either myopia or hyperopia.
    • Prescriptions with myopia (i.e. are nearsighted) will have a MINUS symbol followed by a number (that indicates the amount)
    • Prescriptions with hyperopia (i.e. are farsighted) will have a PLUS symbol followed by a number (which also indicates the amount).
  • Cylindrical (or cyl): Ever heard of the word astigmatism? If you have a number in this column, guess what, you have some form of astigmatism.
    • Depending on the doctor you see, you may have a MINUS symbol here (if you see an optometrist) or a PLUS symbol here (if you see an ophthalmologist) followed by a number (that indicates the amount).
  • Axis: This number indicates the orientation of where your astigmatism is located. This number will be between 0 to 180 degrees and can change over time.
  • Add: This number is for patients that have presbyopia (or focusing/accommodative trouble) and need a bifocal. It provides focusing relieve and will have a PLUS symbol followed by a number (typically between +0.75 and +2.50).

Monday, April 30, 2018

Quick Note: How to find out which Service Pack is installed on SQL Server?

-- SQL Server 2000/2005/2008/2008R2/2012
SELECT  SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')

-- SQL Server 6.5/7.0
SELECT @@VERSION

Friday, April 20, 2018

Duplicate Menu Items in Visual Studio

Recently I had a situation with Visual Studio 2010 where menu items where duplicated like 3-4 times. Looks like some configuration file was corrupted. I have tried to restore current settings and setup new settings but none worked out.

To resolve this I ran the following from command line

devenv.exe /safemode /setup

Once this ran, I restarted Visual Studio I could able to see Visual studio with default factory settings.

PS: If you have any personal settings done before you have to redo those settings again.