C# – How to parse XML with XElement (Linq)

Use the XElement class (from the Linq-to-Xml API) to parse XML and work with it in memory. You can use this to search for XML elements, attributes, and modify values. This is an alternative to using an XML (de)serializer (which requires you to define a class that matches the XML structure). I’ll show examples of … Read more

C# – Read XML element attributes with XElement (Linq)

XML elements can have attributes, which are key-value pairs. To read the attributes, use XElement to parse the XML string (from the Linq-to-Xml API). Then you can use these two methods for getting attributes: Once you have the attributes, use the XAttribute.Value property to read the attribute’s string value. Here’s an example of getting all … Read more

ASP.NET Core – How to receive requests with XML content

Receiving requests with XML content is straightforward. You have to register the built-in XML InputFormatter (otherwise you get 415 – Unsupported Media Type errors). When a request comes in with an XML Content-Type (such as application/xml), it uses this InputFormatter to deserialize the request body XML. In this article, I’ll show step-by-step how to receive … Read more

C# – Find XML element by name with XElement (Linq)

Use the XElement class (from the Linq-to-Xml API) to search for XML elements by name. There are two main methods you can use to do this: These return the matching elements as an IEnumerable<XElement>. You can then use Linq methods (or a foreach loop) to do whatever you want with these elements (such as outputting … Read more

WinForms – How to prompt the user for a file

In a WinForms project, you can prompt the user to select a file by using the OpenFileDialog control: When you call ShowDialog(), it’ll prompt the user to select a file: When the user clicks Open, you’ll be able to get the file path they selected from the OpenFileDialog.FileName property. To use the OpenFileDialog control, drag … Read more

C# – Using XmlSerializer to serialize

Here’s how to serialize an object into XML using XmlSerializer: You must add the [Serializable] attribute to the class you want to serialize: Here’s an example of creating an Author object and feeding it to the serializer: This outputs the following XML: This example showed how to use XmlSerializer with all the default settings. In … Read more

How to generate XML documentation and include it in a nuget package

XML documentation comments serve two purposes: In this article I’ll show how to automatically generate an XML documentation file and how to include it in a nuget package. 1 – Write the XML documentation comments in your code I have a method called MergeInPlace() (which merges two dictionaries in-place). To explain what this is doing … Read more

C# – Read a custom config section from app.config

In this this article, I’ll show the simplest to get a custom config section from app.config and load it into your own config class. You can implement IConfigurationSectionHandler and use ConfigurationManager.GetSection() to do this. I’ll show the steps below. 1 – Add a custom config class The first step is to create a class that’ll … Read more

How to modify app.config at runtime

When you try to modify the app.config at runtime, if you don’t do it right, you’ll run into a few problems: System.Configuration.ConfigurationErrorsException: The configuration is read only. This article will show you how to update the app.config the right way to avoid these problems. This shows three different scenarios: inserting a new connection string, modifying … Read more