Breakpoints cannot be hit when debugging C# in Visual Studio

When this happens, there can be a few reasons

-You attached to the wrong process.

-The build profile was changed to Release instead of Debug, thus no symbols are generated for debugging.

-You opened multiple instances of Visual Studio or programs that lock the PDB file, so when you make a new build the PDB file fails to be regenerated to match the new DLL.

-There are assemblies with classes with same name in the same namespace (for example DI may be configured to load different assemblies depending on some configuration). Visual Studio load the debug file for the wrong assembly when you are debugging some code. VS may assume that there is only one implementation of a a class in same namespace.

Steps to resolve

Make sure you attach to the correct process.

Make sure the build profile is Debug.

Clean and rebuild the solution.

If there are multiple assemblies with classes with the same name, try deleting the one that is not in use and then rebuild. Best option is to avoid classes with same name in same namespace. VS will be confused.

Last step, log out and log in again, or restart the computer to kill the processes that are locking the PDB. It could even be some Visual Studio bug.

This document already has a ‘ DocumentElement ‘ node

I faced this exception while trying to generate a xml document programmatically in C# using XmlDocument and XmlElement.

I didn’t know what was going on, and then I figured out that I was adding multiple elements to the XmlDocument. To solve this, add a “root” element to XmlDocument and then add multiple child elements within the root. The XmlDocument is supposed to only contain 1 child element as expected in XML.

Selecting nodes with no namespace using XPath in C#

When nodes in an XML document has no prefix at all you would think that they are associated with the default namespace, but apparently this is not the case.

Those nodes are considered not associated with any namespace at all. So to select them using XPath, in the NamespaceManager first add the default namespace with any arbitrary prefix of your choosing but not string.Empty, for example:

namespaceManager.AddNameSpace(“x”, “http://…”);

Here x is just a randomly chosen prefix

Then, you can use XPath to select nodes using the prefix you just added

doc.SelectNodes(“//x:NodeName”, namespaceManager);

I spent 2 hours to figure this out, talk about wasting time.

Browsing functionality in a referenced binary

Keyword: object browser in Visual Studio

If you are working on a project with some binaries developed by the company previously and you don’t have access to the source code.

Aside from asking your colleague, use Visual Studio’s Object Explorer to browse the API in referenced binaries without the need of the source code.

Saving settings to app.config for C# winforms app

Follow the following stackoverflow question:

http://stackoverflow.com/questions/30572923/read-all-key-values-from-app-config-c-sharp

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

config.AppSettings.Settings.Remove("MySetting");
config.AppSettings.Settings.Add("MySetting", "some value");

config.Save(ConfigurationSaveMode.Modified);