XmlDocument.LoadXml(String) Metoda

Definicja

Ładuje dokument XML z określonego ciągu.

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)

Parametry

xml
String

Ciąg zawierający dokument XML do załadowania.

Wyjątki

W pliku XML występuje błąd ładowania lub analizowania. W takim przypadku dokument pozostaje pusty.

Przykłady

Poniższy przykład ładuje kod XML do XmlDocument obiektu i zapisuje go w pliku.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew 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 );
   
   // Save the document to a file and auto-indent the output.
   XmlTextWriter^ writer = gcnew XmlTextWriter( "data.xml", nullptr );
   writer->Formatting = Formatting::Indented;
   doc->Save( writer );
}

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

Uwagi

Domyślnie metoda nie zachowuje odstępu LoadXml ani znaczącego odstępu.

Ta metoda analizuje definicje typów dokumentów (DTD), ale nie wykonuje walidacji dtD ani schematu. Jeśli chcesz przeprowadzić walidację, możesz utworzyć wystąpienie sprawdzania poprawności XmlReader przy użyciu XmlReaderSettings klasy i Create metody . Aby uzyskać więcej informacji, zobacz sekcję Uwagi na stronie referencyjnej XmlReader .

Jeśli chcesz załadować z Streamklasy , , StringTextReaderlub XmlReader, użyj metody Load zamiast tej metody.

Ta metoda jest rozszerzeniem Microsoft do modelu DOM (Document Object Model).

Dotyczy

Zobacz też