初学者诚心请教XML的问题.
以下这个XML文件里, 第二行 <bk:Books xmlns:bk=...> 究竟有和意义?
<?xml version= '1.0 ' encoding= 'utf-8 '?>
<bk:Books xmlns:bk= 'http://myserver/myschemas/Books '>
<bk:Book>
<bk:Title> Just XML </bk:Title>
</bk:Book>
<bk:Book>
<bk:Title> Professional XML </bk:Title>
</bk:Book>
<bk:Book>
<bk:Title> XML Step by Step </bk:Title>
</bk:Book>
<bk:Book>
<bk:Title> XML By Example </bk:Title>
</bk:Book>
</bk:Books>
下面这个code中, 其它的都好理解, 就是那个XmlNamespaceManager不知道是怎么个机制, 为什么非要它不可?
protected void Button1_Click(object sender, EventArgs e)
{
XPathDocument doc = new XPathDocument(Server.MapPath( "Demo.xml "));
XPathNavigator nav = doc.CreateNavigator();
XPathExpression xpathExpress = nav.Compile( "//bk:Book[position() "+this.ddownlist.SelectedValue.Trim()+this.txtNum.Text.Trim()+ "] ");
//use AddNamespace
XmlNamespaceManager xmlManager = new XmlNamespaceManager(nav.NameTable);
xmlManager.AddNamespace( "bk ", "http://myserver/myschemas/Books ");
xpathExpress.SetContext(xmlManager);
XPathNodeIterator xIterator = nav.Select(xpathExpress);
this.TextBox1.Text = " ";
this.TextBox1.ForeColor = System.Drawing.Color.Empty;
while (xIterator.MoveNext())
this.TextBox1.Text = this.TextBox1.Text + "\r\n " + xIterator.Current.Value;
if (xIterator.Count.Equals(0))
{
this.TextBox1.Text = "There is no xml data in the condition. ";
this.TextBox1.ForeColor = System.Drawing.Color.Red;
}
xmlManager = null;
nav = null;
doc = null;
}
[解决办法]
xmlns:bk= 'http://myserver/myschemas/Books '是xml的命名空间
[解决办法]
命名空间
[解决办法]
XmlNamespaceManager xmlManager = new XmlNamespaceManager(nav.NameTable);
xmlManager.AddNamespace( "bk ", "http://myserver/myschemas/Books ");
=================
生成命名空间
[解决办法]
xmlns:bk= 'http://myserver/myschemas/Books '是xml的命名空间
不要也行的了