XmlDocument.Load Method (System.Xml) (2024)

  • Reference

Definition

Namespace:
System.Xml
Assemblies:
System.Xml.dll, System.Xml.XmlDocument.dll
Assembly:
System.Xml.ReaderWriter.dll
Assembly:
System.Xml.XmlDocument.dll
Assembly:
System.Xml.dll
Assembly:
netstandard.dll

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Loads the specified XML data from a Stream, a URL, a TextReader, or an XmlReader.

Overloads

Load(Stream)

Loads the XML document from the specified stream.

Load(TextReader)

Loads the XML document from the specified TextReader.

Load(String)

Loads the XML document from the specified URL.

Load(XmlReader)

Loads the XML document from the specified XmlReader.

Load(Stream)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Loads the XML document from the specified stream.

public: virtual void Load(System::IO::Stream ^ inStream);
public virtual void Load (System.IO.Stream inStream);
abstract member Load : System.IO.Stream -> unitoverride this.Load : System.IO.Stream -> unit
Public Overridable Sub Load (inStream As Stream)

Parameters

inStream
Stream

The stream containing the XML document to load.

Exceptions

XmlException

There is a load or parse error in the XML. In this case, a FileNotFoundException is raised.

Remarks

Note

The Load method always preserves significant white space. The PreserveWhitespace property determines whether or not insignificant white space, that is white space in element content, is preserved. The default is false; white space in element content is not preserved.

If you want validation to occur, you can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see the Remarks section of the XmlReader reference page.

This method is a Microsoft extension to the Document Object Model (DOM).

This method automatically detects the string format of the input XML (for example, UTF-8, ANSI, and so on). If your application needs to know which encoding is used to read the stream, consider using an XmlTextReader object to read the stream, and then use the XmlTextReader.Encoding property to determine the encoding. If you need to use a XmlDocument object to work with XML, you can use the XmlTextReader object to create one. For more information, see Reading XML Data using XPathDocument and XmlDocument.

See also

  • ValidationEventArgs
  • XmlSeverityType
  • Schemas

Applies to

Load(TextReader)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Loads the XML document from the specified TextReader.

public: virtual void Load(System::IO::TextReader ^ txtReader);
public virtual void Load (System.IO.TextReader txtReader);
abstract member Load : System.IO.TextReader -> unitoverride this.Load : System.IO.TextReader -> unit
Public Overridable Sub Load (txtReader As TextReader)

Parameters

txtReader
TextReader

The TextReader used to feed the XML data into the document.

Exceptions

XmlException

There is a load or parse error in the XML. In this case, the document remains empty.

Examples

The following example uses the StringReader class to load a string of XML data into the XmlDocument object.

#using <System.Xml.dll>using namespace System;using namespace System::IO;using namespace System::Xml;int main(){ // Create the XmlDocument. XmlDocument^ doc = gcnew XmlDocument; String^ xmlData = "<book xmlns:bk='urn:samples'></book>"; doc->Load( gcnew StringReader( xmlData ) ); // Create a new element and add it to the document. XmlElement^ elem = doc->CreateElement( "bk", "genre", "urn:samples" ); elem->InnerText = "fantasy"; doc->DocumentElement->AppendChild( elem ); Console::WriteLine( "Display the modified XML..." ); doc->Save( Console::Out );}
using System;using System.IO;using System.Xml;public class Sample { public static void Main() { // Create the XmlDocument. XmlDocument doc = new XmlDocument(); string xmlData = "<book xmlns:bk='urn:samples'></book>"; doc.Load(new StringReader(xmlData)); // Create a new element and add it to the document. XmlElement elem = doc.CreateElement("bk", "genre", "urn:samples"); elem.InnerText = "fantasy"; doc.DocumentElement.AppendChild(elem); Console.WriteLine("Display the modified XML..."); doc.Save(Console.Out); }}
Imports System.IOImports System.Xmlpublic class Sample public shared sub Main() ' Create the XmlDocument. Dim doc as XmlDocument = new XmlDocument() Dim xmlData as string = "<book xmlns:bk='urn:samples'></book>" doc.Load(new StringReader(xmlData)) ' Create a new element and add it to the document. Dim elem as XmlElement = doc.CreateElement("bk", "genre", "urn:samples") elem.InnerText = "fantasy" doc.DocumentElement.AppendChild(elem) Console.WriteLine("Display the modified XML...") doc.Save(Console.Out) end subend class

Remarks

Note

The Load method always preserves significant white space. The PreserveWhitespace property determines whether or not insignificant white space, that is white space in element content, is preserved. The default is false; white space in element content is not preserved.

If you want validation to occur, you can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see the Remarks section of the XmlReader reference page.

This method is a Microsoft extension to the Document Object Model (DOM).

See also

  • StreamReader
  • ValidationEventArgs
  • XmlSeverityType
  • Schemas

Applies to

Load(String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Loads the XML document from the specified URL.

public: virtual void Load(System::String ^ filename);
public virtual void Load (string filename);
abstract member Load : string -> unitoverride this.Load : string -> unit
Public Overridable Sub Load (filename As String)

Parameters

filename
String

URL for the file containing the XML document to load. The URL can be either a local file or an HTTP URL (a Web address).

Exceptions

XmlException

There is a load or parse error in the XML. In this case, a FileNotFoundException is raised.

ArgumentException

filename is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

ArgumentNullException

filename is null.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length.

DirectoryNotFoundException

The specified path is invalid (for example, it is on an unmapped drive).

IOException

An I/O error occurred while opening the file.

UnauthorizedAccessException

filename specified a file that is read-only.

-or-

This operation is not supported on the current platform.

-or-

filename specified a directory.

-or-

The caller does not have the required permission.

FileNotFoundException

The file specified in filename was not found.

NotSupportedException

filename is in an invalid format.

SecurityException

The caller does not have the required permission.

Remarks

Note

The Load method always preserves significant white space. The PreserveWhitespace property determines whether or not insignificant white space, that is white space in element content, is preserved. The default is false; white space in element content is not preserved.

If you want validation to occur, you can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see the Remarks section of the XmlReader reference page.

This method is a Microsoft extension to the Document Object Model (DOM).

See also

  • ValidationEventArgs
  • XmlSeverityType
  • Schemas

Applies to

Load(XmlReader)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Loads the XML document from the specified XmlReader.

public: virtual void Load(System::Xml::XmlReader ^ reader);
public virtual void Load (System.Xml.XmlReader reader);
abstract member Load : System.Xml.XmlReader -> unitoverride this.Load : System.Xml.XmlReader -> unit
Public Overridable Sub Load (reader As XmlReader)

Parameters

reader
XmlReader

The XmlReader used to feed the XML data into the document.

Exceptions

XmlException

There is a load or parse error in the XML. In this case, the document remains empty.

Examples

The following example loads the last book node of the books.xml file into the XML document.

#using <System.Xml.dll>using namespace System;using namespace System::IO;using namespace System::Xml;int main(){ //Create the XmlDocument. XmlDocument^ doc = gcnew XmlDocument; //Load the document with the last book node. XmlTextReader^ reader = gcnew XmlTextReader( "books.xml" ); reader->WhitespaceHandling = WhitespaceHandling::None; reader->MoveToContent(); reader->Read(); reader->Skip(); //Skip the first book. reader->Skip(); //Skip the second book. doc->Load( reader ); doc->Save( Console::Out );}
using System;using System.IO;using System.Xml;public class Sample{ public static void Main() { //Create the XmlDocument. XmlDocument doc = new XmlDocument(); //Load the document with the last book node. XmlTextReader reader = new XmlTextReader("books.xml"); reader.WhitespaceHandling = WhitespaceHandling.None; reader.MoveToContent(); reader.Read(); reader.Skip(); //Skip the first book. reader.Skip(); //Skip the second book. doc.Load(reader); doc.Save(Console.Out); }}
Option ExplicitOption StrictImports System.IOImports System.XmlPublic Class Sample Public Shared Sub Main() 'Create the XmlDocument. Dim doc As New XmlDocument() 'Load the document with the last book node. Dim reader As New XmlTextReader("books.xml") reader.WhitespaceHandling = WhitespaceHandling.None reader.MoveToContent() reader.Read() reader.Skip() 'Skip the first book. reader.Skip() 'Skip the second book. doc.Load(reader) doc.Save(Console.Out) End SubEnd Class

The example uses the file, books.xml, as input.

<?xml version='1.0'?><!-- This file represents a fragment of a book store inventory database --><bookstore> <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book></bookstore>

Remarks

Note

The Load method always preserves significant white space. The PreserveWhitespace property determines whether or not insignificant white space, that is white space in element content, is preserved. The default is false; white space in element content is not preserved.

If the reader is in the initial state (ReadState =ReadState.Initial), Load consumes the entire contents of the reader and builds the DOM from what it finds.

If the reader is already positioned on some node at depth "n", this method loads that node and all subsequent siblings up to the end tag that closes depth "n". This has the following results.

If the current node and its siblings look like the following:

<!--comment--><element1>one</element1><element2>two</element2>

Load throws an exception because a document cannot have two root level elements. If the current node and its siblings look like the following:

<!--comment--><?process instruction?><!--comment--></endtag>

Load succeeds, but you have an incomplete DOM tree because there is no root level element. Before you save the document, you must add a root level element, otherwise Save will throw an exception.

If the reader is positioned on a leaf node that is invalid for the root level of a document, for example a white space or attribute node, the reader continues to read until it is positioned on a node that can be used for the root. The document begins loading at this point.

If you want validation to occur, you can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see the Remarks section of the XmlReader reference page.

This method is a Microsoft extension to the Document Object Model (DOM).

See also

  • ValidationEventArgs
  • XmlSeverityType
  • Schemas

Applies to

XmlDocument.Load Method (System.Xml) (2024)
Top Articles
A Truly Talented Surgeon
Motivating Social Influencers to Engage in Health Behavior Interventions
Sugar And Spice 1976 Pdf
Wal-Mart 2516 Directory
Jeff Bezos Lpsg
Csuf Mail
Culver's Flavor Of The Day Ann Arbor
Petco Westerly Ri
Autozone Locations Near Me
Superhot Unblocked Games
Cherry Downloadcenter
Phil Maloof Net Worth
Jak zgłosić awarię i brak energii elektrycznej w Twoim mieszkaniu lub domu? - ENERGA-OPERATOR SA
Erhöhte Gesundheitsgefahr durch Zuckeraustauschstoff Erythrit?
Po Box 6726 Portland Or 97228
Yovanis Pizzeria - View Menu & Order Online - 741 NY-211 East, Middletown, NY 10941 - Slice
Busted Newspaper Hart County Ky
Who is Ariana Grande? Everything You Need to Know
Nearest Walmart Address
5Ive Brother Cause Of Death
Lebenszahl 8: Ihre wirkliche Bedeutung
Pritzker Sdn 2023
The Woman King Showtimes Near Cinemark 14 Lancaster
630251.S - CCB-PWRIO-05 - Vision Systems - Vision Systems In-Sight, Cognex - InSight 2800 Series - Accessories Cables / Brackets IS28XX -
Ups Store Pineville La
Chi Trib Weather
Hcpss Staff Hub Workday
A vintage funfair / fairground
Reapers Tax Barotrauma
Violetken 5E
Takeaways from AP's report updating the cult massacre that claimed hundreds of lives in Kenya
Search results for: Kert\u00E9sz, Andr\u00E9, page 1
South Park Old Fashioned Gif
Bdo Passion Of Valtarra
Gracex Rayne
On-Campus Student Employment
Walmart Tune Up Near Me
Acadis Portal Indiana Sign In
Sim7 Bus Time
Ruth Chris 3 Course Meal
Opsb Pay Dates
Craigslist Philly Free Stuff
O'reilly's In Mathis Texas
Mychart Mountainstarhealth
Under One Shining Stone Another Lies
Mexican cartel leader 'El Mayo' Zambada pleads not guilty to US charges
Leader of multi-state identity fraud ring sentenced to federal prison
Reli Stocktwits
Kohl's Hixson Tennessee
11 Fascinating Axolotl Facts
29+ Des Moines Craigslist Furniture
Bbw Chan Lmbb
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 5658

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.