Surendra Sharma

Surendra Sharma

Search This Blog

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, March 26, 2023

Sitecore with CSharp REPL

If you're a C# developer, you'll want to check out this exciting new tool: C# REPL! This cross-platform command line tool is designed for rapid experimentation and exploration of C# code. With C# REPL, you can easily test out snippets of code, try out different syntaxes, and even experiment with new packages.

One of the great things about C# REPL is that it supports intellisense, making it easy to quickly find the methods and properties you need. You can also install NuGet packages directly from the command line, so you can easily add new functionality to your code without having to switch to a different tool.

C# REPL is a .NET 7 global tool, which means it can be easily installed on any machine running Windows 10, Mac OS, or Linux. And because it's a command line tool, it's lightweight and easy to use - perfect for developers who want to streamline their workflow and focus on writing great code.

With its support for referencing local .NET projects and assemblies, C# REPL can be a valuable tool for exploring and experimenting with Sitecore-specific code and functionality.

Here I am showing a demo on how you can use it Sitecore libraries to test it.

Check out this video for the same


Whether you're a seasoned C# developer or just starting out, C# REPL is a must-have tool for your toolkit. Give it a try today and see how it can help you work more efficiently and experiment more quickly!

Sunday, March 24, 2019

Code : Get any attribute value from HTML string in C#


If you have HTML string in C# and you want to get any particular value of any attribute then you can use below function which refer Regex to get its value.
In below program I am extraing “src” attribute value of HTML “img" tag.

Input string : <img alt="" src="/MediaCenter/PublishImages/DSC_0134.jpg" width="150" style="border:0px solid" />

Output : /MediaCenter/PublishImages/DSC_0134.jpg

Code:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string s = "<img alt=\"\" src=\"/MediaCenter/PublishImages/DSC_0134.jpg\" width=\"150\" style=\"border:0px solid\" />";

            var srcs = GetSrcInHTMLImgString(s);
            Console.WriteLine(srcs[0]);//Output: /MediaCenter/PublishImages/DSC_0134.jpg
        }

        public static List<string> GetSrcInHTMLImgString(string htmlString)
        {
            List<string> srcs = new List<string>();
            string pattern = @"(?<=src="").*?(?="")";
           
            Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection matches = rgx.Matches(htmlString);

            for (int i = 0, l = matches.Count; i < l; i++)
            {
                string d = matches[i].Value;
                srcs.Add(d);
            }
            return srcs;
        }
    }
}

We can make this function more generic where we will pass any attribute name and get the value as

public static List<string> GetAttributeNameInHTMLString(string htmlString, string attributeName)
{
    List<string> attributeValues = new List<string>();
    string pattern = string.Format(@"(?<={0}="").*?(?="")", attributeName);

    Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
    MatchCollection matches = rgx.Matches(htmlString);

    for (int i = 0, l = matches.Count; i < l; i++)
    {
        string d = matches[i].Value;
        attributeValues.Add(d);
    }
    return attributeValues;
}

Calling this function as

string s = "<img alt=\"\" src=\"/MediaCenter/PublishingImages/DSC_0134.jpg\" width=\"150\" style=\"border:0px solid\" />";

var widths = GetAttributeNameInHTMLString(s, "width");
Console.WriteLine(widths[0]);   //Output : 150

var styles = GetAttributeNameInHTMLString(s, "style");
Console.WriteLine(styles[0]);   //Output : border:0px solid

Its a small function but very handy. 

To test it, you can use C# online compiler “.NET Fiddle” instead of creating console application in Visual Studio. Many times I am using these online tools for quick testing. 

Let me know if you have any better idea to get attribute value from HTML string.

Friday, October 19, 2018

ebook : SharePoint to Sitecore: Strategy and Implementation


Sharing my 6th eBook with Sitecore community - "SharePoint to Sitecore: Strategy and Implementation".

SharePoint to Sitecore
SharePoint to Sitecore


There are multiple ways to move data and images from SharePoint to Sitecore. I tried my best to include all the approaches but explained one approach which worked for me in Sitecore 9 update 1 instance.

You can read and download this ebook from here:


I hope you like this eBook.

Please leave your comments or share this eBook if it’s useful for you.