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

XML doc warnings: CS1573 and CS1572

I’ll show examples of the XML documentation warnings and how to fix them. CS1573 Parameter ‘X’ has no matching param tag in the XML comment (but other parameters do) Warning CS1573 means there’s a missing <param> tag for one of the method parameters. This usually happens when you add a new parameter (or rename one … 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