Compartilhar via


XMLNode.LoadXml Method

Definition

Populates an XMLNode control with data.

Overloads

LoadXml(XmlElement)

Populates an XMLNode control with data from an XmlElement.

LoadXml(XmlDocument)

Populates an XMLNode control with data from the root node of the specified XmlDocument.

LoadXml(String)

Populates an XMLNode control with data from the specified XML string.

Remarks

This method does not add additional XMLNode controls or delete unnecessary XMLNode controls from the document.

This method updates all attributes of the XMLNode control.

LoadXml(XmlElement)

Populates an XMLNode control with data from an XmlElement.

public:
 void LoadXml(System::Xml::XmlElement ^ xmlElement);
public void LoadXml (System.Xml.XmlElement xmlElement);
abstract member LoadXml : System.Xml.XmlElement -> unit
Public Sub LoadXml (xmlElement As XmlElement)

Parameters

xmlElement
XmlElement

The XML element that contains the data.

Examples

The following code example uses the LoadXml method to fill an XMLNode with data from an XmlElement. The example reads the contents of an XML file into a StreamReader, loads this StreamReader into an XmlDocument, and then initializes an XmlElement that represents the first child element in the XmlDocument. This XmlElement is then used for the xmlElement parameter of the LoadXml method. This example assumes that the current document contains an XMLNode named CustomerLastNameNode that corresponds to the first child element in the associated schema. This example also assumes that an XML file named Customers.xml, which conforms to the associated schema, exists at the root of the D directory.

private void LoadXmlFromXmlElement()
{
    string xmlPath = @"D:\Customers.xml";
    System.IO.StreamReader xmlStreamReader = 
        new System.IO.StreamReader(xmlPath);
    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

    xmlDoc.Load(xmlStreamReader);
    System.Xml.XmlElement firstElement =
        (System.Xml.XmlElement)xmlDoc.DocumentElement.FirstChild;
    this.CustomerLastNameNode.LoadXml(firstElement);
}
Private Sub LoadXmlFromXmlElement()
    Dim xmlPath As String = "D:\Customers.xml"
    Dim xmlStreamReader As New System.IO.StreamReader(xmlPath)
    Dim xmlDoc As New System.Xml.XmlDocument()

    xmlDoc.Load(xmlStreamReader)
    Dim firstElement As System.Xml.XmlElement = _
        CType(xmlDoc.DocumentElement.FirstChild, System.Xml.XmlElement)
    Me.CustomerLastNameNode.LoadXml(firstElement)
End Sub

Remarks

This method does not add additional XMLNode controls or delete unnecessary XMLNode controls from the document.

This method updates all attributes of the XMLNode control.

Applies to

LoadXml(XmlDocument)

Populates an XMLNode control with data from the root node of the specified XmlDocument.

public:
 void LoadXml(System::Xml::XmlDocument ^ document);
public void LoadXml (System.Xml.XmlDocument document);
abstract member LoadXml : System.Xml.XmlDocument -> unit
Public Sub LoadXml (document As XmlDocument)

Parameters

document
XmlDocument

The XmlDocument that contains the data.

Examples

The following code example uses the LoadXml method to fill an XMLNode with data from an XmlDocument. The example reads the contents of an XML file into a StreamReader, loads this StreamReader into an XmlDocument, and then uses the XmlDocument for the document parameter of the LoadXml method. This example assumes that the current document contains an XMLNode named CustomerNode. This example also assumes that an XML file named Customers.xml, which conforms to the schema associated with CustomerNode, exists at the root of the D directory.

private void LoadXmlFromXmlDocument()
{
    string xmlPath = @"D:\Customers.xml";
    System.IO.StreamReader xmlStreamReader = 
        new System.IO.StreamReader(xmlPath);
    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

    xmlDoc.Load(xmlStreamReader);
    this.CustomerNode.LoadXml(xmlDoc);
}
Private Sub LoadXmlFromXmlDocument()
    Dim xmlPath As String = "D:\Customers.xml"
    Dim xmlStreamReader As New System.IO.StreamReader(xmlPath)
    Dim xmlDoc As New System.Xml.XmlDocument()

    xmlDoc.Load(xmlStreamReader)
    Me.CustomerNode.LoadXml(xmlDoc)
End Sub

Remarks

This method does not add additional XMLNode controls or delete unnecessary XMLNode controls from the document.

This method updates all attributes of the XMLNode control.

Applies to

LoadXml(String)

Populates an XMLNode control with data from the specified XML string.

public:
 void LoadXml(System::String ^ xml);
public void LoadXml (string xml);
abstract member LoadXml : string -> unit
Public Sub LoadXml (xml As String)

Parameters

xml
String

The XML string to read data from.

Examples

The following code example uses the LoadXml method to fill an XMLNode control with data from an XML string. This example assumes that the current document contains an XMLNode named CustomerNode, and the namespace and elements described in the XML string conform to the schema associated with CustomerNode.

private void LoadXmlFromString()
{
    this.CustomerNode.LoadXml(
        "<Customer xmlns='http://tempuri.org/XMLNodeCSCustomers.xsd'>" +
        "<LastName>Smith</LastName></Customer>");
}
Private Sub LoadXmlFromString()
    Me.CustomerNode.LoadXml( _
        "<Customer xmlns='http://tempuri.org/XMLNodeVBCustomers.xsd'>" & _
        "<LastName>Smith</LastName></Customer>")
End Sub

Remarks

This method does not add additional XMLNode controls or delete unnecessary XMLNode controls from the document.

This method updates all attributes of the XMLNode control.

Applies to