XmlTextWriter.WriteStartAttribute(String, String, String) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
특성의 시작을 작성합니다.
public:
override void WriteStartAttribute(System::String ^ prefix, System::String ^ localName, System::String ^ ns);
public override void WriteStartAttribute (string? prefix, string localName, string? ns);
public override void WriteStartAttribute (string prefix, string localName, string ns);
override this.WriteStartAttribute : string * string * string -> unit
Public Overrides Sub WriteStartAttribute (prefix As String, localName As String, ns As String)
매개 변수
- prefix
- String
특성의 Namespace
접두사입니다.
- localName
- String
특성의 LocalName
입니다.
- ns
- String
특성의 NamespaceURI
입니다.
예외
localName
가 null
또는 String.Empty
입니다.
예제
다음 예제에서는 책을 씁니다.
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
String^ filename = "sampledata.xml";
XmlTextWriter^ writer = gcnew XmlTextWriter( filename, nullptr );
//Use indenting for readability.
writer->Formatting = Formatting::Indented;
writer->WriteComment( "sample XML fragment" );
//Write an element (this one is the root).
writer->WriteStartElement( "bookstore" );
//Write the namespace declaration.
writer->WriteAttributeString( "xmlns", "bk", nullptr, "urn:samples" );
writer->WriteStartElement( "book" );
//Lookup the prefix and then write the ISBN attribute.
String^ prefix = writer->LookupPrefix( "urn:samples" );
writer->WriteStartAttribute( prefix, "ISBN", "urn:samples" );
writer->WriteString( "1-861003-78" );
writer->WriteEndAttribute();
//Write the title.
writer->WriteStartElement( "title" );
writer->WriteString( "The Handmaid's Tale" );
writer->WriteEndElement();
//Write the price.
writer->WriteElementString( "price", "19.95" );
//Write the style element.
writer->WriteStartElement( prefix, "style", "urn:samples" );
writer->WriteString( "hardcover" );
writer->WriteEndElement();
//Write the end tag for the book element.
writer->WriteEndElement();
//Write the close tag for the root element.
writer->WriteEndElement();
//Write the XML to file and close the writer.
writer->Flush();
writer->Close();
//Read the file back in and parse to ensure well formed XML.
XmlDocument^ doc = gcnew XmlDocument;
//Preserve white space for readability.
doc->PreserveWhitespace = true;
//Load the file
doc->Load( filename );
//Write the XML content to the console.
Console::Write( doc->InnerXml );
}
using System;
using System.IO;
using System.Xml;
public class Sample
{
private const string filename = "sampledata.xml";
public static void Main()
{
XmlTextWriter writer = new XmlTextWriter (filename, null);
//Use indenting for readability.
writer.Formatting = Formatting.Indented;
writer.WriteComment("sample XML fragment");
//Write an element (this one is the root).
writer.WriteStartElement("bookstore");
//Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");
writer.WriteStartElement("book");
//Lookup the prefix and then write the ISBN attribute.
string prefix = writer.LookupPrefix("urn:samples");
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
writer.WriteString("1-861003-78");
writer.WriteEndAttribute();
//Write the title.
writer.WriteStartElement("title");
writer.WriteString("The Handmaid's Tale");
writer.WriteEndElement();
//Write the price.
writer.WriteElementString("price", "19.95");
//Write the style element.
writer.WriteStartElement(prefix, "style", "urn:samples");
writer.WriteString("hardcover");
writer.WriteEndElement();
//Write the end tag for the book element.
writer.WriteEndElement();
//Write the close tag for the root element.
writer.WriteEndElement();
//Write the XML to file and close the writer.
writer.Flush();
writer.Close();
//Read the file back in and parse to ensure well formed XML.
XmlDocument doc = new XmlDocument();
//Preserve white space for readability.
doc.PreserveWhitespace = true;
//Load the file
doc.Load(filename);
//Write the XML content to the console.
Console.Write(doc.InnerXml);
}
}
Option Strict
Option Explicit
Imports System.IO
Imports System.Xml
Public Class Sample
Private Shared filename As String = "sampledata.xml"
Public Shared Sub Main()
Dim writer As New XmlTextWriter(filename, Nothing)
'Use indenting for readability.
writer.Formatting = Formatting.Indented
writer.WriteComment("sample XML fragment")
'Write an element (this one is the root).
writer.WriteStartElement("bookstore")
'Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:samples")
writer.WriteStartElement("book")
'Lookup the prefix and then write the ISBN attribute.
Dim prefix As String = writer.LookupPrefix("urn:samples")
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples")
writer.WriteString("1-861003-78")
writer.WriteEndAttribute()
'Write the title.
writer.WriteStartElement("title")
writer.WriteString("The Handmaid's Tale")
writer.WriteEndElement()
'Write the price.
writer.WriteElementString("price", "19.95")
'Write the style element.
writer.WriteStartElement(prefix, "style", "urn:samples")
writer.WriteString("hardcover")
writer.WriteEndElement()
'Write the end tag for the book element.
writer.WriteEndElement()
'Write the close tag for the root element.
writer.WriteEndElement()
'Write the XML to file and close the writer.
writer.Flush()
writer.Close()
'Read the file back in and parse to ensure well formed XML.
Dim doc As New XmlDocument()
'Preserve white space for readability.
doc.PreserveWhitespace = True
'Load the file.
doc.Load(filename)
'Write the XML content to the console.
Console.Write(doc.InnerXml)
End Sub
End Class
설명
참고
.NET Framework 2.0부터는 메서드와 XmlWriterSettings 클래스를 사용하여 XmlWriter.Create 새 기능을 활용하여 인스턴스를 만드는 XmlWriter 것이 좋습니다.
이 버전은 와 같은 WriteString여러 쓰기 메서드를 사용하여 특성 값을 작성할 수 있는 의 고급 버전 WriteAttributeString 입니다.
적용 대상
추가 정보
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET