Creates a new instance of the XmlReader class.
public static XmlReader Create(string uri);
public static XmlReader Create(string uri, XmlReaderSettings settings);
public static XmlReader Create(Stream input);
public static XmlReader Create(Stream input, XmlReaderSettings settings);
public static XmlReader Create(TextReader input);
public static XmlReader Create(TextReader input, XmlReaderSettings settings);
public static XmlReader Create(Uri uri);
public static XmlReader Create(Uri uri, XmlReaderSettings settings);
public static XmlReader Create(string uri, XmlReaderSettings settings, XmlParserContext context);
public static XmlReader Create(Stream input, XmlReaderSettings settings, XmlParserContext context);
public static XmlReader Create(TextReader input, XmlReaderSettings settings, XmlParserContext context);
null, the default parsing rules are used.
A new XmlReader object.
The XmlReader.Create method is a factory method used to create an instance of the XmlReader class. It supports creating readers from various sources, including URIs, Streams, and TextReaders.
When creating an XmlReader, you can optionally provide an XmlReaderSettings object to customize the parsing behavior. This includes settings for:
The XmlParserContext parameter is used to provide information about the current XML document, such as the base URI, namespace manager, and XML declaration properties. This is useful when reading fragments of XML or when dealing with specific parsing scenarios.
// Example 1: Creating an XmlReader from a URI
using (XmlReader reader = XmlReader.Create("http://www.example.com/data.xml"))
{
while (reader.Read())
{
// Process the XML nodes
if (reader.NodeType == XmlNodeType.Element)
{
Console.WriteLine($"Element: {reader.Name}");
}
}
}
// Example 2: Creating an XmlReader from a Stream with settings
string xmlString = "<root><item>Hello</item></root>";
byte[] xmlBytes = System.Text.Encoding.UTF8.GetBytes(xmlString);
using (Stream stream = new MemoryStream(xmlBytes))
using (XmlReaderSettings settings = new XmlReaderSettings())
{
settings.IgnoreWhitespace = true;
using (XmlReader reader = XmlReader.Create(stream, settings))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Text)
{
Console.WriteLine($"Text: {reader.Value}");
}
}
}
}
ArgumentNullException: If uri is null.XmlException: If the XML document is not well-formed.Applies to: