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

Check if XML nodes are in correct order

Details
Written by: Stanko Milosev
Category: C#
Published: 24 April 2014
Last Updated: 15 September 2020
Hits: 4932
  • xml

I had task to check if XML nodes are in correct order. After some research basically all I needed was this line:

var nodes = myXml.Elements().ToList();

So, code would look something like this:

var nodes = myXml.Elements().ToList();

var expectedOrder = new[] { "first", "second", "third", "fourth" }.ToList();

var actualOrder = nodes.Select(node => node.Name.LocalName).ToList();

Assert.AreEqual(string.Join(",", expectedOrder), string.Join(",", actualOrder));

Moq mock and setup

Details
Written by: Stanko Milosev
Category: C#
Published: 10 April 2014
Last Updated: 10 April 2014
Hits: 5398

Mocks are "fake" objects, that means that with mock you can create object which will have fake values.
For example, lets declare one interface without implentation, something like:

public interface ICalculator
{
	int Summation(int a, int b);
}


Now, our test method will look like something like this:

public void SetupMoqExampleTestMethod()
{
	var mock = new Mock<ICalculator>();
	mock.Setup(m => m.Summation(It.IsAny<int>(), It.IsAny<int>())).Returns(10);
	Assert.AreEqual(mock.Object.Summation(1, 1), 10);
}


Notice the line:

mock.Setup(m => m.Summation(It.IsAny<int>(), It.IsAny<int>())).Returns(10);


With that line we said that which ever parameter we send to the "Summation" method, result will be always 10.
That is why our test will not fail.

Example you can download from here.

 

Constructor with base keyword

Details
Written by: Stanko Milosev
Category: C#
Published: 10 April 2014
Last Updated: 10 April 2014
Hits: 4106

Create new console project, and create base class like this:

public class BaseClass
{
	public BaseClass(int value)
	{
		Console.WriteLine("BaseClass constructor with value: " + value.ToString());
	}
}


Now, let's derive class from our BaseClass:

class DerivedClass: BaseClass
{
	public DerivedClass(int value)
	: base(value)
	{
		Console.WriteLine("DerivedClass constructor with value: " + value.ToString());
	}
}


If we create our Main method like this:

class Program
{
	static void Main(string[] args)
	{
		BaseClass a = new BaseClass(10);
		DerivedClass b = new DerivedClass(20);
		Console.ReadKey();
	}
}


Result will be like this:

BaseClass constructor with value: 10
BaseClass constructor with value: 20
DerivedClass constructor with value: 20

Example project you can download from here.

Indexer example

Details
Written by: Stanko Milosev
Category: C#
Published: 10 April 2014
Last Updated: 10 April 2014
Hits: 4255

Start new console and create one class like this:

public class IndexerClass
{
	private string[] myData;

	public string this[string myTest]
	{
		get
		{
			return "Test get";
		}
	}
}


Now if Main method looks like this:

class Program
{
	static void Main(string[] args)
	{
		IndexerClass myInd = new IndexerClass();

		Console.WriteLine(myInd["A test"]);

		Console.ReadKey();
	}
}


Result would be something like this:

Test get

Example you can download from here

  1. "Using" c# keyword
  2. More about XML creating in C#
  3. Serialization and deserialization
  4. EntityHydrate task failed

Subcategories

WPF

Beginning

Code snippets

NUnit

LINQ

Windows Forms

Page 14 of 33

  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18