XPathNavigator.CreateAttributes Metoda

Definicja

XmlWriter Zwraca obiekt używany do tworzenia nowych atrybutów w bieżącym elemencie.

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

Zwraca

Obiekt XmlWriter używany do tworzenia nowych atrybutów w bieżącym elemencie.

Wyjątki

Element XPathNavigator nie jest umieszczony w węźle elementu.

Element XPathNavigator nie obsługuje edycji.

Przykłady

W poniższym przykładzie nowe discount atrybuty i currency są tworzone w price elemecie podrzędnym pierwszego book elementu w contosoBooks.xml pliku przy użyciu XmlWriter obiektu zwróconego CreateAttributes z metody .

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)

W przykładzie plik jest contosoBooks.xml pobierany jako dane wejściowe.

<?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>  

Uwagi

Poniżej przedstawiono ważne uwagi, które należy wziąć pod uwagę podczas korzystania z CreateAttributes metody .

  • Po umieszczeniu elementu XPathNavigator na elemencie nowe atrybuty utworzone przez XPathNavigator metodę są umieszczane na końcu listy atrybutów bieżącego elementu.

  • Nowe atrybuty nie są wstawiane do momentu Close wywołania metody XmlWriter obiektu.

  • Jeśli określony prefiks przestrzeni nazw to String.Empty lub null, prefiks identyfikatora URI przestrzeni nazw nowego atrybutu jest uzyskiwany z bieżących przestrzeni nazw w zakresie. Jeśli nie ma prefiksu przestrzeni nazw przypisanego do określonego identyfikatora URI przestrzeni nazw w bieżącym zakresie, prefiks przestrzeni nazw jest generowany automatycznie. Aby na przykład utworzyć nowy atrybut dla elementu w domyślnej przestrzeni nazw contosoBooks.xml pliku (xmlns="http://www.contoso.com/books"), należy określić null lub String.Empty dla parametrów prefiksu przestrzeni nazw i identyfikatora URI przestrzeni nazw. Określenie http://www.contoso.com/books jako parametru identyfikatora URI przestrzeni nazw spowoduje CreateAttribute , że metoda automatycznie wygeneruje prefiks przestrzeni nazw dla nowego atrybutu.

  • Jeśli nowy atrybut utworzony jest węzłem przestrzeni nazw, który powoduje konflikt z deklaracją przestrzeni nazw w elemecie , ponieważ wybrany prefiks przestrzeni nazw jest używany przez inną deklarację przestrzeni nazw w tym samym zakresie lub dlatego, że wybrany prefiks jest taki sam jak prefiks elementu, ale jest powiązany z innym identyfikatorem URI przestrzeni nazw, zgłaszany jest wyjątek.

  • Zwrócony XmlWriter obiekt może służyć tylko do tworzenia atrybutów. Wywoływanie innych XmlWriter metod obiektów, które nie tworzą atrybutów zgłasza wyjątek.

  • Metoda CreateAttributes nie ma wpływu na położenie XPathNavigatorobiektu .

Dotyczy