Button1_Click 事件中的代码应显示如下: XmlDocument oxmldoc = new XmlDocument();
try
{
oxmldoc.Load("c:\\Books.xml");
XPathNavigator oXPathNav;
oXPathNav = oxmldoc.CreateNavigator();
XPathExpression Expr;
Expr = oXPathNav.Compile("//bk:Book[position()>=2]");
XmlNamespaceManager oxmlNSManager = new XmlNamespaceManager(oXPathNav.NameTable);
oxmlNSManager.AddNamespace("bk", "http://myserver/myschemas/Books");
Expr.SetContext(oxmlNSManager);
XPathNodeIterator iterator = oXPathNav.Select(Expr);
while (iterator.MoveNext())
{
this.textBox1.Text = this.textBox1.Text + "\r\n"+ iterator.Current.Value ; //在这里已经查找并移动到符合条件的节点,在这里就可以进行我们想要的操作像插入,更新,删除等. //iterator.Current.InsertAfter("<xs:element name=\"newitem\" type=\"xs:string\" />");插入
}
oxmlNSManager = null;
oXPathNav = null;
oxmldoc = null;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
|