XmlDocument.LoadXml(String) Metódus

Definíció

Betölti az XML-dokumentumot a megadott sztringből.

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

Paraméterek

xml
String

A betöltendő XML-dokumentumot tartalmazó karakterlánc.

Kivételek

Terhelési vagy elemzési hiba történt az XML-ben. Ebben az esetben a dokumentum üres marad.

Példák

Az alábbi példa betölti az XML-t egy XmlDocument objektumba, és menti egy fájlba.

using System;
using System.Xml;

public class Sample {

  public static void Main() {

    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<item><name>wrench</name></item>");

   // Add a price element.
   XmlElement newElem = doc.CreateElement("price");
   newElem.InnerText = "10.95";
   doc.DocumentElement.AppendChild(newElem);

   XmlWriterSettings settings = new XmlWriterSettings();
   settings.Indent = true;
   // Save the document to a file and auto-indent the output.
   XmlWriter writer = XmlWriter.Create("data.xml", settings);
    doc.Save(writer);
  }
}
Imports System.Xml

public class Sample 

  public shared sub Main() 
 
    ' Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<item><name>wrench</name></item>")

   ' Add a price element.
   Dim newElem as XmlElement = doc.CreateElement("price")
   newElem.InnerText = "10.95"
   doc.DocumentElement.AppendChild(newElem)

   Dim settings As New XmlWriterSettings()
   settings.Indent = True
   ' Save the document to a file and auto-indent the output.
   Dim writer As XmlWriter = XmlWriter.Create("data.xml", settings)
    doc.Save(writer)
  end sub
end class

Megjegyzések

Alapértelmezés szerint a metódus nem őrzi meg a LoadXml fehér területet vagy a jelentős szabad területet.

Ez a metódus elemzi a dokumentumtípus-definíciókat (DTD-ket), de nem végez DTD- vagy sémaérvényesítést. Ha ellenőrzést szeretne végezni, létrehozhat egy érvényesítő XmlReader példányt az osztály és a XmlReaderSettingsCreate metódus használatával. További információt a referenciaoldal Megjegyzések szakaszában XmlReader talál.

Ha be szeretne tölteni egy Stream, String, TextReadervagy XmlReader, használja a Betöltés metódust a metódus helyett.

Ez a metódus a Dokumentumobjektum-modell (DOM) Microsoft bővítménye.

A következőre érvényes:

Lásd még