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

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