XmlDocument.LoadXml(String) 메서드

정의

지정된 문자열에서 XML 문서를 로드합니다.

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)

매개 변수

xml
String

로드할 XML 문서가 포함된 문자열입니다.

예외

XML에 로드 또는 구문 분석 오류가 있습니다. 이 경우 문서는 빈 상태로 유지됩니다.

예제

다음 예제에서는 개체에 XML을 XmlDocument 로드하고 파일에 저장합니다.

#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

설명

기본적으로 이 메서드는 LoadXml 공백이나 상당한 공백을 유지하지 않습니다.

이 메서드는 DTD(문서 형식 정의)를 구문 분석하지만 DTD 또는 스키마 유효성 검사는 수행하지 않습니다. 유효성 검사를 수행하려면 클래스와 Create 메서드를 사용하여 유효성 XmlReader 검사 인스턴스를 XmlReaderSettings 만들 수 있습니다. 자세한 내용은 XmlReader 참조 페이지의 설명 섹션을 참조하세요.

에서 StringTextReader로드Stream하려는 경우 또는 XmlReader이 메서드 대신 Load 메서드를 사용합니다.

이 메서드는 DOM(문서 개체 모델)에 대한 Microsoft 확장입니다.

적용 대상

추가 정보