संपादित करें

इसके माध्यम से साझा किया गया


XmlDataDocument.Load Method

Definition

Loads the XmlDataDocument using the specified data source and synchronizes the DataSet with the loaded data.

Overloads

Load(Stream)

Loads the XmlDataDocument from the specified stream.

Load(TextReader)

Loads the XmlDataDocument from the specified TextReader.

Load(String)

Loads the XmlDataDocument using the specified URL.

Load(XmlReader)

Loads the XmlDataDocument from the specified XmlReader.

Remarks

Note

In order to view the XML data relationally, you must first specify a schema to use for data mapping. This can be done either by calling the ReadXmlSchema method or by creating the tables and columns within the DataSet manually. This step must be done before calling Load.

XmlDataDocument does not support creating entity references. If the data includes entity references, the Load method resolves and expands any entity references. However, if you are using the Load overload that takes a XmlReader as an argument, you must specify an XmlReader that can resolve entities.

Load(Stream)

Source:
XmlDataDocument.cs
Source:
XmlDataDocument.cs
Source:
XmlDataDocument.cs

Loads the XmlDataDocument from the specified stream.

public:
 override void Load(System::IO::Stream ^ inStream);
public override void Load (System.IO.Stream inStream);
override this.Load : System.IO.Stream -> unit
Public Overrides Sub Load (inStream As Stream)

Parameters

inStream
Stream

The stream containing the XML document to load.

Remarks

XmlDataDocument does not support creating entity references. If the data includes entity references, the Load method resolves and expands any entity references.

Note

In order to view the XML data relationally, you must first specify a schema to use for data mapping. This can be done either by calling the ReadXmlSchema method or by creating the tables and columns within the DataSet manually. This step must be done before calling Load.

Applies to

Load(TextReader)

Source:
XmlDataDocument.cs
Source:
XmlDataDocument.cs
Source:
XmlDataDocument.cs

Loads the XmlDataDocument from the specified TextReader.

public:
 override void Load(System::IO::TextReader ^ txtReader);
public override void Load (System.IO.TextReader txtReader);
override this.Load : System.IO.TextReader -> unit
Public Overrides Sub Load (txtReader As TextReader)

Parameters

txtReader
TextReader

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

Remarks

XmlDataDocument does not support creating entity references. If the data includes entity references, the Load method resolves and expands any entity references.

Note

In order to view the XML data relationally, you must first specify a schema to use for data mapping. This can be done either by calling the ReadXmlSchema method or by creating the tables and columns within the DataSet manually. This step must be done before calling Load.

Applies to

Load(String)

Source:
XmlDataDocument.cs
Source:
XmlDataDocument.cs
Source:
XmlDataDocument.cs

Loads the XmlDataDocument using the specified URL.

public:
 override void Load(System::String ^ filename);
public override void Load (string filename);
override this.Load : string -> unit
Public Overrides Sub Load (filename As String)

Parameters

filename
String

The URL of the file containing the XML document to load.

Remarks

XmlDataDocument does not support creating entity references. If the data includes entity references, the Load method resolves and expands any entity references.

Note

In order to view the XML data relationally, you must first specify a schema to use for data mapping. This can be done either by calling the ReadXmlSchema method or by creating the tables and columns within the DataSet manually. This step must be done before calling Load.

Applies to

Load(XmlReader)

Source:
XmlDataDocument.cs
Source:
XmlDataDocument.cs
Source:
XmlDataDocument.cs

Loads the XmlDataDocument from the specified XmlReader.

public:
 override void Load(System::Xml::XmlReader ^ reader);
public override void Load (System.Xml.XmlReader reader);
override this.Load : System.Xml.XmlReader -> unit
Public Overrides Sub Load (reader As XmlReader)

Parameters

reader
XmlReader

The XmlReader containing the XML document to load.

Exceptions

The XML being loaded contains entity references, and the reader cannot resolve entities.

Examples

The following example modifies the price of a book using the DataSet methods.

#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Data;
using namespace System::Xml;
int main()
{
   // Create an XmlDataDocument.
   XmlDataDocument^ doc = gcnew XmlDataDocument;

   // Load the schema file.
   doc->DataSet->ReadXmlSchema( "store.xsd" );

   // Load the XML data.
   XmlTextReader^ reader = gcnew XmlTextReader( "2books.xml" );
   reader->MoveToContent(); // Moves the reader to the root node.
   doc->Load( reader );

   // Update the price on the first book using the DataSet methods.
   DataTable^ books = doc->DataSet->Tables["book"];
   books->Rows[0]["price"] = "12.95";
   Console::WriteLine( "Display the modified XML data..." );
   doc->Save( Console::Out );
}
using System;
using System.Data;
using System.Xml;
public class Sample {
    public static void Main() {
        // Create an XmlDataDocument.
        XmlDataDocument doc = new XmlDataDocument();

        // Load the schema file.
        doc.DataSet.ReadXmlSchema("store.xsd");

        // Load the XML data.
        XmlTextReader reader = new XmlTextReader("2books.xml");
        reader.MoveToContent(); // Moves the reader to the root node.
        doc.Load(reader);

        // Update the price on the first book using the DataSet methods.
        DataTable books = doc.DataSet.Tables["book"];
        books.Rows[0]["price"] = "12.95";

        Console.WriteLine("Display the modified XML data...");
        doc.Save(Console.Out);
    }
} // End class
Imports System.Data
Imports System.Xml

public class Sample

  public shared sub Main()

      'Create an XmlDataDocument.
      Dim doc as XmlDataDocument = new XmlDataDocument()

      'Load the schema.
      doc.DataSet.ReadXmlSchema("store.xsd") 
 
      'Load the XML data.
      Dim reader as XmlTextReader = new XmlTextReader("2books.xml")
      reader.MoveToContent() 'Moves the reader to the root node.
      doc.Load(reader)
        
     'Change the price on the first book imports the DataSet methods.
     Dim books as DataTable = doc.DataSet.Tables.Item("book")
     books.Rows.Item(0).Item("price") = "12.95"
        
     Console.WriteLine("Display the modified XML data...")
     doc.Save(Console.Out)

  end sub
end class

The example uses the following two input files.

2books.xml

<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

store.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>

 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

Remarks

XmlDataDocument does not support creating entity references. If the data source contains entity references, you must create an XmlValidatingReader with the EntityHandling property set to EntityHandling.ExpandEntities (this is the default behavior) and pass the XmlValidatingReader to the Load method. If you do not use an XmlValidatingReader, the Load method throws an exception.

The Load method always preserves significant white space. The PreserveWhitespace property determines whether or not white space is preserved. The default is false, white space is not preserved.

If the reader is in the initial state (that is, 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", then 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 following siblings look similar to 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 following siblings look similar to the following:

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

Load will succeed; however, you will have an incomplete DOM tree, because there is no root-level element. You have to add a root-level element before you save the document; otherwise, the Save method throws 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.

Applies to