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 contenente il documento XML da caricare.

Eccezioni

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

Esempio

Nell'esempio seguente il codice XML viene caricato in un XmlDocument oggetto e salvato in un file.

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 spazi vuoti o spazi vuoti significativi.

Questo metodo analizza le definizioni dei tipi di documento (DTD), ma non esegue la convalida DTD o dello schema. Se si vuole eseguire la convalida, è possibile creare un'istanza di XmlReader convalida 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 , String, TextReadero XmlReader, usare il metodo Load anziché questo metodo.

Questo metodo è un'estensione Microsoft al dom (Document Object Model).

Si applica a

Vedi anche