Class StringDecoratorExtensions
Extension methods for the string class hidden behind the IDecorator<T> interface.
public static class StringDecoratorExtensions
- Inheritance
-
StringDecoratorExtensions
Examples
StringDecoratorExtensions in the Xml.Linq namespace provides extension methods on Decorator.Enclose for validating and parsing XML strings into XElement objects. This example checks valid XML ("<root><item id=\"1\">value</item></root>") with IsXmlString returning True, parses it via TryParseXElement and navigates to read the root name, item attribute, and value. Invalid XML ("not xml at all") returns False for IsXmlString and TryParseXElement returns false with null. It also demonstrates TryParseXElement with LoadOptions.PreserveWhitespace to retain whitespace in the parsed output. Console output confirms each validation and parsing result.
using System;
using System.Xml.Linq;
using Cuemon;
using Cuemon.Xml.Linq;
namespace MyApp.Xml
{
public class StringDecoratorExtensionsExample
{
public void Demonstrate()
{
// Valid XML string
var validXml = "<root><item id=\"1\">value</item></root>";
// Check if the string is valid XML
var isValid = Decorator.Enclose(validXml).IsXmlString();
Console.WriteLine($"Is valid XML: {isValid}"); // True
// Try to parse the XML string into an XElement
if (Decorator.Enclose(validXml).TryParseXElement(out var element))
{
Console.WriteLine($"Root element: {element.Name}");
Console.WriteLine($"Inner XML: {element}");
// Navigate the parsed XElement
var item = element.Element("item");
Console.WriteLine($"Item id attribute: {item?.Attribute("id")?.Value}");
Console.WriteLine($"Item value: {item?.Value}");
// Invalid XML string
var invalidXml = "not xml at all";
var isInvalid = Decorator.Enclose(invalidXml).IsXmlString();
Console.WriteLine($"Is valid XML: {isInvalid}"); // False
// TryParse will return false for invalid XML
if (!Decorator.Enclose(invalidXml).TryParseXElement(out var badElement))
{
Console.WriteLine("Could not parse invalid XML.");
Console.WriteLine($"Result is null: {badElement == null}"); // True
// TryParse with load options
var xmlWithWhitespace = " <root><item>value</item></root> ";
if (Decorator.Enclose(xmlWithWhitespace)
.TryParseXElement(LoadOptions.PreserveWhitespace, out var preserveElement))
{
Console.WriteLine($"Preserved whitespace element: '{preserveElement}'");
}}}}}
}
Methods
IsXmlString(IDecorator<string>)
Determines whether the enclosed string of the specified decorator is a valid XML string.
public static bool IsXmlString(this IDecorator<string> decorator)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
Returns
- bool
trueif the enclosed string of the specifieddecoratoris a valid XML string; otherwise,false.
TryParseXElement(IDecorator<string>, LoadOptions, out XElement)
Tries to load an XElement from the enclosed string of the specified decorator that contains XML, optionally preserving white space and retaining line information.
public static bool TryParseXElement(this IDecorator<string> decorator, LoadOptions options, out XElement result)
Parameters
decoratorIDecorator<string>The IDecorator<T> 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 enclosed string of the specified
decoratorthat contains XML, if the conversion succeeded, or a null reference if the conversion failed.
Returns
- bool
trueif the enclosed string of the specifieddecoratorwas converted successfully; otherwise,false.
TryParseXElement(IDecorator<string>, out XElement)
public static bool TryParseXElement(this IDecorator<string> decorator, out XElement result)
Parameters
decoratorIDecorator<string>The IDecorator<T> to extend.
resultXElementWhen this method returns, it contains the XElement populated from the enclosed string of the specified
decoratorthat contains XML, if the conversion succeeded, or a null reference if the conversion failed.
Returns
- bool
trueif the enclosed string of the specifieddecoratorwas converted successfully; otherwise,false.