XmlDocument.LoadXml(String) Metodo

Definizione

Carica il documento XML dalla stringa specificata.

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)

Parametri

xml
String

Stringa che contiene il documento XML da caricare.

Eccezioni

Si è verificato un errore di analisi o di caricamento nel codice XML. In questo caso, il documento rimane vuoto.

Esempio

L'esempio seguente carica XML in un oggetto e lo salva in un XmlDocument file.

#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

Commenti

Per impostazione predefinita, il LoadXml metodo non mantiene spazio vuoto o spazio vuoto significativo.

Questo metodo analizza le definizioni dei tipi di documento (DTD), ma non esegue la convalida DTD o schema. Se si vuole che venga eseguita la convalida, è possibile creare un'istanza di convalida XmlReader usando la XmlReaderSettings classe e il Create metodo . Per altre informazioni, vedere la sezione Note della pagina di riferimento XmlReader.

Se si vuole caricare da un Streammetodo , , TextReaderStringo XmlReader, usare il metodo Load anziché questo metodo.

Questo metodo è un'estensione Microsoft per il modello a oggetti document (DOM).

Si applica a

Vedi anche