XPathNavigator.CreateAttributes Yöntem

Tanım

Geçerli öğede yeni öznitelikler oluşturmak için kullanılan bir XmlWriter nesneyi 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.

XPathNavigator düzenlemeyi desteklemiyor.

Örnekler

Aşağıdaki örnekte, discount yönteminden döndürülen currency nesnesi kullanılarak, price dosyasındaki ilk book öğesinin contosoBooks.xml alt öğesinde yeni XmlWriter ve CreateAttributes öznitelikleri oluşturulur.

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, contosoBooks.xml dosyasını giriş olarak alır.

<?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ırken dikkate alınması gereken önemli notlar aşağıdadır CreateAttributes .

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

  • Nesnenin yöntemi Close çağrılana XmlWriter kadar yeni öznitelikler eklenmez.

  • Belirtilen ad alanı öneki veya String.Emptyisenull, yeni özniteliğin ad alanı URI'sinin ön eki kapsamdaki 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 bulunan bir öğede yeni bir öznitelik oluşturmak için (xmlns="http://www.contoso.com/books" ), hem ad alanı ön eki hem de ad alanı URI parametrelerini belirtirsiniz nullString.Empty . Ad alanı URI parametresi olarak belirtilmesi http://www.contoso.com/books yöntemin CreateAttribute yeni öznitelik için bir ad alanı ön eki oluşturmasına neden olur.

  • Oluşturulan yeni öznitelik, seçilen ad alanı öneki 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 çakan 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.

  • yöntemi, CreateAttributes konumunu XPathNavigatoretkilemez.

Şunlara uygulanır