XmlWriter.WriteAttributes(XmlReader, Boolean) XmlWriter.WriteAttributes(XmlReader, Boolean) XmlWriter.WriteAttributes(XmlReader, Boolean) XmlWriter.WriteAttributes(XmlReader, Boolean) Method


在衍生類別中覆寫時,寫出在 XmlReader 的目前位置找到的所有屬性。When overridden in a derived class, writes out all the attributes found at the current position in the XmlReader.

 virtual void WriteAttributes(System::Xml::XmlReader ^ reader, bool defattr);
public virtual void WriteAttributes (System.Xml.XmlReader reader, bool defattr);
abstract member WriteAttributes : System.Xml.XmlReader * bool -> unit
override this.WriteAttributes : System.Xml.XmlReader * bool -> unit
Public Overridable Sub WriteAttributes (reader As XmlReader, defattr As Boolean)


XmlReader XmlReader XmlReader XmlReader

要複製屬性的 XmlReaderThe XmlReader from which to copy the attributes.

Boolean Boolean Boolean Boolean

若要從 true 複製預設屬性,則為 XmlReader,否則為 falsetrue to copy the default attributes from the XmlReader; otherwise, false.


讀取器不在 elementattributeXmlDeclaration 節點上。The reader is not positioned on an element, attribute or XmlDeclaration node.

在先前的非同步作業完成前呼叫了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."


下列範例會將所有項目複製到輸出,將標記名稱變更為大寫,並將保持不變的所有屬性都複製。The following example copies all the elements to the output, changes the tag names to upper case, and copies all the attributes unchanged.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
void main()
   XmlReader^ reader = XmlReader::Create( L"test1.xml" );
   XmlWriterSettings^ settings = gcnew XmlWriterSettings;
   settings->Indent = true;
   XmlWriter^ writer = XmlWriter::Create( Console::Out );
   while ( reader->Read() )
      if ( reader->NodeType == XmlNodeType::Element )
         writer->WriteStartElement( reader->Name->ToUpper() );
         writer->WriteAttributes( reader, false );
         if ( reader->IsEmptyElement )
      if ( reader->NodeType == XmlNodeType::EndElement )

using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {
    XmlReader reader = XmlReader.Create("test1.xml");
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    XmlWriter writer = XmlWriter.Create(Console.Out);

    while (reader.Read()) {
      if (reader.NodeType == XmlNodeType.Element) {
        writer.WriteAttributes(reader, false);
        if (reader.IsEmptyElement) writer.WriteEndElement();
      else if (reader.NodeType == XmlNodeType.EndElement) {
Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
  Public Shared Sub Main()

    Dim reader As XmlReader = XmlReader.Create("test1.xml")
    Dim settings As XmlWriterSettings = new XmlWriterSettings()
    settings.Indent = true
    Dim writer As XmlWriter = XmlWriter.Create(Console.Out)
      While reader.Read()
          If reader.NodeType = XmlNodeType.Element Then
              writer.WriteAttributes(reader, False)
              If reader.IsEmptyElement Then
              End If
              If reader.NodeType = XmlNodeType.EndElement Then
              End If
          End If
      End While

    End Sub 'Main
End Class 'Sample

此範例會使用該檔案,test1.xml作為輸入。The example uses the file, test1.xml, as input.

<test a="1" b="2">
    <item c="3" d="4" e="5" f="6"/>


如果讀取器置於element節點WriteAttributes複製所有包含的屬性。If the reader is positioned on an element node WriteAttributes copies all the contained attributes. 如果讀取器置於attribute 節點,這個方法會寫入目前的屬性,則其餘的屬性項目結尾標記之前。If the reader is positioned on an attribute node, this method writes the current attribute, then the rest of the attributes until the element closing tag. 如果讀取器置於XmlDeclaration 節點,這個方法會寫入所有屬性宣告中。If the reader is positioned on an XmlDeclaration node, this method writes all the attributes in the declaration. 如果讀取器定位於適用於任何其他節點類型這個方法會擲回XmlExceptionIf the reader is positioned on any other node type this method throws an XmlException.

如果呼叫此方法使用XmlValidatingReader,以確保格式正確的 XML 寫入時,會取代任何內容 (其已擴充的實體),可能會導致無效的文件。If this method is called using XmlValidatingReader, to ensure well-formed XML any content (which has been expanded from the entities) that could result in an invalid document is replaced when written. 例如,如果屬性包含&gt;已經過擴充,以確保格式正確的文件展開的實體 > 以寫出時,取代&gt;For example, if an attribute includes an &gt; entity that has been expanded, to ensure a well-formed document the expanded > is replaced when written out with &gt;.

這個方法的非同步版本,請參閱WriteAttributesAsyncFor the asynchronous version of this method, see WriteAttributesAsync.