Class StringExtensions
- Namespace
- Cuemon.Extensions.Xml.Linq
- Assembly
- Cuemon.Extensions.Xml.dll
Extension methods for the string class.
public static class StringExtensions
- Inheritance
-
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
IsXmlString(string)
Determines whether the specified value is a valid XML string.
public static bool IsXmlString(this string value)
Parameters
Returns
- bool
trueif the specifiedvalueis a valid XML string; otherwise,false.
TryParseXElement(string, LoadOptions, out XElement)
Tries to load an XElement from a value that contains XML, optionally preserving white space and retaining line information.
public static bool TryParseXElement(this string value, LoadOptions options, out XElement result)
Parameters
valuestringThe string to extend.
optionsLoadOptionsA LoadOptions that specifies white space behavior, and whether to load base URI and line information.
resultXElementWhen this method returns, it contains the XElement populated from the
valuethat contains XML, if the conversion succeeded, or a null reference (Nothing in Visual Basic) if the conversion failed.
Returns
- bool
trueif thevalueparameter was converted successfully; otherwise,false.
TryParseXElement(string, out XElement)
Tries to load an XElement from a value that contains XML.
public static bool TryParseXElement(this string value, out XElement result)
Parameters
valuestringThe string to extend.
resultXElementWhen this method returns, it contains the XElement populated from the
valuethat contains XML, if the conversion succeeded, or a null reference (Nothing in Visual Basic) if the conversion failed.
Returns
- bool
trueif thevalueparameter was converted successfully; otherwise,false.