milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home

Creating valid HTTP URI

Details
Written by: Stanko Milosev
Category: C#
Published: 20 February 2022
Last Updated: 20 February 2022
Hits: 249
One example how to create URI with HTTP protocol. If we have like "www.milosev.com" and link is "2015-01-23-20-08-55/gallery", then safest way is to do it like:
Uri domain = new UriBuilder("milosev.com").Uri;

if (Uri.TryCreate(domain, "2015-01-23-20-08-55/gallery", out Uri myUri))
{
	Console.WriteLine(myUri.AbsoluteUri);
}

Sitemap example

Details
Written by: Stanko Milosev
Category: C#
Published: 20 February 2022
Last Updated: 29 May 2022
Hits: 255
  • xml
Here is one my example on how to create basic sitemap using serialization XML should look like:
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.example.com/</loc>
    <lastmod>2005-01-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>
The model:
using System.Collections.Generic;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace SiteMapXmlCreation
{
    public class Model
    {
        [XmlRoot("urlset")]
        public class Urlset
        {
          [XmlAttribute("schemaLocation", Namespace = XmlSchema.InstanceNamespace)]
          public string SchemaLocation { get; set; }
            [XmlElement(ElementName = "url")] public List<url> Url { get; set; }
        }

        public class url
        {
            [XmlElement(ElementName = "loc")] public string Loc { get; set; }

            [XmlElement(ElementName = "lastmod")] public string Lastmod { get; set; }

            [XmlElement(ElementName = "changefreq")] public string Changefreq { get; set; }

            [XmlElement(ElementName = "priority")] public string Priority { get; set; }
        }
    }
}
The Program:
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace SiteMapXmlCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            Model.Urlset serializedUrlset = new Model.Urlset
            {
                Url = new List<Model.url> {
                    new() {
                        Changefreq = "monthly"
                        , Lastmod = "2005-01-01"
                        , Loc = "http://www.example.com/"
                        , Priority = "0.8"
                    }
                }
                , SchemaLocation = "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
            };

            TextWriter txtWriter = new StreamWriter(Path.ChangeExtension(System.Reflection.Assembly.GetEntryAssembly().Location, ".xml"));

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add(string.Empty, "http://www.sitemaps.org/schemas/sitemap/0.9");
            ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            ns.Add("xhtml", "http://www.w3.org/1999/xhtml");

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Model.Urlset), "http://www.sitemaps.org/schemas/sitemap/0.9");
            xmlSerializer.Serialize(txtWriter, serializedUrlset, ns);

            txtWriter.Close();
        }
    }
}
Example download from here.

Await and async

Details
Written by: Stanko Milosev
Category: C#
Published: 19 February 2022
Last Updated: 20 February 2022
Hits: 256
  • core
One example of await and async from the book Pro C# 9 with .NET 5: Foundational Principles and Practices in Programming
using System;
using System.Threading;
using System.Threading.Tasks;

namespace AwaitAsyncExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Task.Run(async () =>
            {
                await MultipleAwaits();
            });

            Console.ReadKey();
        }

        static async Task MultipleAwaits()
        {
            Task task1 = Task.Run(() =>
            {
                Thread.Sleep(2_000);
                Console.WriteLine("Done with first task!");
            });
            Task task2 = Task.Run(() =>
            {
                Thread.Sleep(1_000);
                Console.WriteLine("Done with second task!");
            });
            Task task3 = Task.Run(() =>
            {
                Thread.Sleep(1_000);
                Console.WriteLine("Done with third task!");
            });
            await Task.WhenAll(task1, task2, task3);
        }
    }
}
Another example:
private static readonly Object ThisLock = new Object();

static void Main(string[] args)
{
  Task.Run(async () =>
  {
	await MultipleAwaits();
  });

  Console.ReadKey();
}

static async Task MultipleAwaits()
{
  Task task1 = Task.Run(() =>
  {
	lock (ThisLock)
	{
	  File.AppendAllText("test.txt", "test 1");
	}
  });
  Task task2 = Task.Run(() =>
  {
	lock (ThisLock)
	{
	  File.AppendAllText("test.txt", "test 2");
	}
  });
  Task task3 = Task.Run(() =>
  {
	lock (ThisLock)
	{
	  File.AppendAllText("test.txt", "test 3");
	}
  });
  await Task.WhenAll(task1, task2, task3);
}
Notice:
lock (ThisLock)
{
  File.AppendAllText("test.txt", "test 2");
}
Without lock exception "The process cannot access the file 'test.txt' because it is being used by another process." will be raised.

Master - Detail example

Details
Written by: Stanko Milosev
Category: C#
Published: 21 November 2021
Last Updated: 30 November 2021
Hits: 480
Here is one my example of in-memory DataTables master - detail relationship.
Create table with code like:
DataTable dtMaster = new DataTable("Master");
In this example I will add Columns without specifying type:
dtMaster.Columns.Add("Id");
Create new row:
DataRow drMaster = dtMaster.NewRow();
drMaster["Id"] = 1;
Add row to DataTable:
dtMaster.Rows.Add(drMaster);
Now details table:
DataTable dtDetail = new DataTable("Details");
dtDetail.Columns.Add("Id");
dtDetail.Columns.Add("FkDetailMasterId");

DataRow drDetail = dtDetail.NewRow();
drDetail["Id"] = 1;
drDetail["FkDetailMasterId"] = 1;
dtDetail.Rows.Add(drDetail);
Create DataSet and add relation:
DataSet ds = new DataSet();

ds.Tables.Add(dtMaster);
ds.Tables.Add(dtDetail);

DataRelation relMasterDetail = new DataRelation("MyRelation"
	, ds.Tables["Master"].Columns["Id"]
	, ds.Tables["Details"].Columns["FkDetailMasterId"]
);

ds.Relations.Add(relMasterDetail);
Example download from here.
  1. Invoke private method with reflection
  2. Example of IEnumerable casting
  3. Filter XML nodes per value
  4. Few thread examples

Subcategories

C#

Azure

ASP.NET

JavaScript

Software Development Philosophy

MS SQL

IBM WebSphere MQ

MySQL

Joomla

Delphi

PHP

Windows

Life

Lazarus

Downloads

Android

CSS

Chrome

HTML

Linux

Eclipse

Page 2 of 137

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10