Share via


XPathNavigator.CreateAttributes Yöntem

Tanım

Geçerli öğede yeni öznitelikler oluşturmak için kullanılan nesneyi XmlWriter döndürür.

public:
 virtual System::Xml::XmlWriter ^ CreateAttributes();
public virtual System.Xml.XmlWriter CreateAttributes ();
abstract member CreateAttributes : unit -> System.Xml.XmlWriter
override this.CreateAttributes : unit -> System.Xml.XmlWriter
Public Overridable Function CreateAttributes () As XmlWriter

Döndürülenler

XmlWriter Geçerli öğede yeni öznitelikler oluşturmak için kullanılan nesne.

Özel durumlar

XPathNavigator bir öğe düğümünde konumlandırılmaz.

düzenlemeyi XPathNavigator desteklemez.

Örnekler

Aşağıdaki örnekte, new discount ve currency öznitelikleri, yönteminden CreateAttributes döndürülen nesnesi kullanılarak XmlWriter dosyadaki contosoBooks.xml ilk book öğenin alt öğesinde oluşturulurprice.

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");
navigator->MoveToChild("price", "http://www.contoso.com/books");

XmlWriter^ attributes = navigator->CreateAttributes();

attributes->WriteAttributeString("discount", "1.00");
attributes->WriteAttributeString("currency", "USD");
attributes->Close();

navigator->MoveToParent();
Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");
navigator.MoveToChild("price", "http://www.contoso.com/books");

XmlWriter attributes = navigator.CreateAttributes();

attributes.WriteAttributeString("discount", "1.00");
attributes.WriteAttributeString("currency", "USD");
attributes.Close();

navigator.MoveToParent();
Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")
navigator.MoveToChild("price", "http://www.contoso.com/books")

Dim attributes As XmlWriter = navigator.CreateAttributes()

attributes.WriteAttributeString("discount", "1.00")
attributes.WriteAttributeString("currency", "USD")
attributes.Close()

navigator.MoveToParent()
Console.WriteLine(navigator.OuterXml)

Örnek, dosyayı giriş olarak alır contosoBooks.xml .

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

Açıklamalar

Yöntemi kullanılırken CreateAttributes dikkate alınması gereken önemli notlar aşağıdadır.

  • XPathNavigator öğesinde konumlandırıldığında, yöntemi tarafından XPathNavigator oluşturulan yeni öznitelikler geçerli öğenin öznitelik listesinin sonuna yerleştirilir.

  • Yeni öznitelikler, nesnesinin yöntemi çağrılana CloseXmlWriter kadar eklenmez.

  • Belirtilen ad alanı ön eki veya nulliseString.Empty, yeni özniteliğin ad alanı URI'sinin ön eki kapsam içindeki geçerli ad alanından alınır. Geçerli kapsamda belirtilen ad alanı URI'sine atanmış bir ad alanı ön eki yoksa, otomatik olarak bir ad alanı ön eki oluşturulur. Örneğin, dosyanın varsayılan ad alanında contosoBooks.xml bir öğe üzerinde yeni bir öznitelik oluşturmak için, ()xmlns="http://www.contoso.com/books" hem ad alanı ön eki hem de ad alanı URI parametreleri için veya String.Empty belirtirsiniznull. Ad alanı URI parametresi olarak belirtilmesi http://www.contoso.com/books , yönteminin CreateAttribute yeni öznitelik için bir ad alanı ön eki oluşturmasına neden olur.

  • Oluşturulan yeni öznitelik, seçilen ad alanı ön eki aynı kapsamdaki başka bir ad alanı bildirimi tarafından kullanıldığından veya seçilen ön ek öğeninkiyle aynı olduğundan ancak farklı bir ad alanı URI'sine bağlı olduğundan öğedeki bir ad alanı bildirimiyle çakişen bir ad alanı düğümüyse, bir özel durum oluşturulur.

  • Döndürülen XmlWriter nesne yalnızca öznitelik oluşturmak için kullanılabilir. Öznitelik oluşturmayan diğer XmlWriter nesne yöntemlerini çağırmak bir özel durum oluşturur.

  • CreateAttributes yöntemi, konumunu XPathNavigatoretkilemez.

Şunlara uygulanır