Class XmlDocumentFactory
Provides access to factory methods for creating and configuring XmlDocument instances.
public static class XmlDocumentFactory
- Inheritance
-
XmlDocumentFactory
Examples
The following example parses an XML string into an XmlDocument using XmlDocumentFactory, then navigates the document to read the root element name and a book's title attribute. The output displays "books" and "1984".
using System;
using System.Xml;
using Cuemon.Xml;
namespace Cuemon.Xml;
public class XmlDocumentFactoryExample
{
public void Demonstrate()
{
var xml = "<?xml version=\"1.0\"?><books><book title=\"1984\" author=\"Orwell\"/></books>";
var doc = XmlDocumentFactory.CreateDocument(xml);
var root = doc.DocumentElement;
Console.WriteLine($"Root element: {root?.Name}");
var book = root?.SelectSingleNode("book");
Console.WriteLine($"Title: {book?.Attributes?["title"]?.Value}");
}
}
Methods
CreateDocument(Stream, bool)
Creates and returns an instance of XmlDocument from the specified stream.
public static XmlDocument CreateDocument(Stream stream, bool leaveOpen = false)
Parameters
streamStreamThe Stream to convert.
leaveOpenboolif
true, the Stream object is being left open; otherwise it is being closed and disposed.
Returns
- XmlDocument
An XmlDocument initialized with the XML provided by
stream.
Exceptions
- ArgumentNullException
streamcannot be null.
CreateDocument(string)
Creates and returns an instance of XmlDocument from the specified value.
public static XmlDocument CreateDocument(string value)
Parameters
Returns
- XmlDocument
An XmlDocument initialized with the XML provided by
value.
Exceptions
- ArgumentNullException
valuecannot be null.- ArgumentException
valuecannot be empty or consist only of white-space characters.
CreateDocument(Uri)
Creates and returns an instance of XmlDocument from the specified uriLocation.
public static XmlDocument CreateDocument(Uri uriLocation)
Parameters
Returns
- XmlDocument
An XmlDocument initialized with the XML provided by
uriLocation.
Exceptions
- ArgumentNullException
uriLocationcannot be null.
CreateDocument(XmlReader, bool)
Creates and returns an instance of XmlDocument from the specified reader.
public static XmlDocument CreateDocument(XmlReader reader, bool leaveOpen = false)
Parameters
readerXmlReaderThe XmlReader to convert.
leaveOpenboolif
true, the XmlReader object is being left open; otherwise it is being closed and disposed.
Returns
- XmlDocument
An XmlDocument initialized with the XML provided by
reader.
Exceptions
- ArgumentNullException
readercannot be null.