Table of Contents

StringExtensions Class

Definition

Namespace
Cuemon.Extensions.Xml.Linq
Assemblies
Cuemon.Extensions.Xml.dll
Source
src/Cuemon.Extensions.Xml/Linq/StringExtensions.cs

Extension methods for the string class.

public static class StringExtensions
Inheritance
object
StringExtensions

Examples

The following example demonstrates how to parse XML strings into XElement using the StringExtensions class.

using System;
using System.Xml.Linq;
using Cuemon.Extensions.Xml.Linq;

namespace MyApp.Examples;

public class StringExtensionsExample
{
    public void Demonstrate()
    {
        string xml = "<root><item id=\"1\">Value</item></root>";

        // Try to parse the XML string into an XElement
        if (xml.TryParseXElement(out XElement element))
        {
            Console.WriteLine(element.Name); // root
            Console.WriteLine(element.Element("item")?.Value); // Value

        // Check if a string is valid XML
        bool isValid = xml.IsXmlString();
        Console.WriteLine(isValid); // True

        // Invalid XML returns false
        string invalid = "<not-valid>";
        bool isInvalid = invalid.IsXmlString();
        Console.WriteLine(isInvalid); // False

}}
}

Methods

Name Description
IsXmlString(string)

Determines whether the specified value is a valid XML string.

TryParseXElement(string, LoadOptions, out XElement)

Tries to load an XElement from a value that contains XML, optionally preserving white space and retaining line information.

TryParseXElement(string, out XElement)

Tries to load an XElement from a value that contains XML.