在.NET 下读取.xml 文件的方法:
在.NET 下读取.xml 文件的方法:
1.利用 XmlTextReader 类,如:
XmlTextReader reader = new XmlTextReader(Server.MapPath(XmlPath));
while(reader.Read())
{
if(reader.NodeType == XmlNodeType.Text )
{
Response.Write(reader.Name +"dd"+ reader.Value);
}
}
2,利用 XPathNavigator 类:
XPathDocument xpdoc = new XPathDocument(Server.MapPath(XmlPath),XmlSpace.Preserve);
XPathNavigator xpNav = xpdoc.CreateNavigator();
XPathNodeIterator xpIter = xpNav.SelectDescendants("USERNAME",String.Empty,true);
//Response.Write(xpIter.Current.Name + xpIter.Current.Value);
while(xpIter.MoveNext())
{
if(xpIter.Current.Value == "c21")
{}
}
3,利用 XmlDocument 类:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath(XmlPath));
XmlNamespaceManager mgr = new XmlNamespaceManager(xdoc.NameTable);
mgr.AddNamespace("c21","http://52089.net");
XmlNodeList xList =xdoc.SelectNodes("//USER",mgr);
foreach(XmlNode node in xList)
{
if(node.ChildNodes[0].InnerText == "c21")
{
Response.Write("春天来了!");
}
}
4,利用 DataSet 类:
DataSet dsUsers = new DataSet();
dsUsers.ReadXmlSchema(pubPath +"\\UserInfo.xsd"); //如果有的话
dsUsers.ReadXml(pubPath +"\\UserInfo.xml"); 必须有!
有了DataSet对象后,什么都好办了,如:
DataRow rowUser = dsUsers.Tables["User"].Rows.Find(userName); //获得符合条件的数据行对象