IHasXmlNode 接口

使类可以从当前上下文或位置返回 XmlNode

**命名空间:**System.Xml
**程序集:**System.Xml(在 system.xml.dll 中)

语法

声明
Public Interface IHasXmlNode
用法
Dim instance As IHasXmlNode
public interface IHasXmlNode
public interface class IHasXmlNode
public interface IHasXmlNode
public interface IHasXmlNode

备注

IHasXmlNode 接口提供一个接口,后者使类可以从当前上下文或位置返回 XmlNode。它是由 XPathNavigator 对象实现的,这些对象对具有 XmlNode 节点的类进行操作。例如,如果 XPathNavigator 对象是由 XmlDocument 创建的,则可以使用 GetNode 方法返回表示导航器当前位置的 XmlNode

示例

下面的示例使用 GetNode 方法检索并修改所选的节点。

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath

public class Sample

  public shared sub Main()

     Dim doc as XmlDocument = new XmlDocument()
     doc.Load("books.xml")
                         
     ' Create an XPathNavigator and select all books by Plato.
     Dim nav as XPathNavigator = doc.CreateNavigator()
     Dim ni as XPathNodeIterator = nav.Select("descendant::book[author/name='Plato']")
     ni.MoveNext()

     ' Get the first matching node and change the book price.
     Dim book as XmlNode = CType(ni.Current, IHasXmlNode).GetNode()
     book.LastChild.InnerText = "12.95"
     Console.WriteLine(book.OuterXml)
    
  end sub
end class
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;

public class Sample
{
  public static void Main()
  {
     XmlDocument doc = new XmlDocument();
     doc.Load("books.xml");
                         
     // Create an XPathNavigator and select all books by Plato.
     XPathNavigator nav = doc.CreateNavigator();
     XPathNodeIterator ni = nav.Select("descendant::book[author/name='Plato']");
     ni.MoveNext();

     // Get the first matching node and change the book price.
     XmlNode book = ((IHasXmlNode)ni.Current).GetNode();
     book.LastChild.InnerText = "12.95";
     Console.WriteLine(book.OuterXml);
    
  }
}
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::XPath;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( "books.xml" );
   
   // Create an XPathNavigator and select all books by Plato.
   XPathNavigator^ nav = doc->CreateNavigator();
   XPathNodeIterator^ ni = nav->Select("descendant::book[author/name='Plato']");
   ni->MoveNext();
   
   // Get the first matching node and change the book price.
   XmlNode^ book = dynamic_cast<IHasXmlNode^>(ni->Current)->GetNode();
   book->LastChild->InnerText = "12.95";
   Console::WriteLine( book->OuterXml );
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.XPath.*;

public class Sample
{
    public static void main(String[] args)
    {
        XmlDocument doc = new XmlDocument();

        doc.Load("books.xml");

        // Create an XPathNavigator and select all books by Plato.
        XPathNavigator nav = doc.CreateNavigator();
        XPathNodeIterator ni = nav.
            Select("descendant::book[author/name='Plato']");
        ni.MoveNext();

        // Get the first matching node and change the book price.
        XmlNode book = ((IHasXmlNode)(ni.get_Current())).GetNode();

        book.get_LastChild().set_InnerText("12.95");
        Console.WriteLine(book.get_OuterXml());
    } //main 
} //Sample

该示例使用文件 books.xml 作为输入。

<bookstore>
  <book genre="autobiography" publicationdate="1981" 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" 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" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

IHasXmlNode 成员
System.Xml 命名空间