xml文件如何读取
有一个xml文件,如下:
<?xml version="1.0" encoding="utf-8" ?>
<customerInfo>
<NO1>
<name>testusd</customerAccount>
<age>12</customerName>
<address>testusd</address>
</NO1>
<NO2>
<name>test</customerAccount>
<age>19</customerName>
<address>test</address>
</NO2>
</customerInfo>
在页面中有一个table,如下:
<table border ="0" cellpadding ="0" cellspacing ="0" runat="server" width ="100%" style ="margin-top:20px">
<tr align="right" >
<td style="height: 22px">
<asp:TextBox runat ="server" ID="txtName" Text="姓名"
Width ="300px" CssClass ="textBoxStyle" BackColor="Silver" Font-Bold="True"></asp:TextBox></td>
</tr>
<tr align ="right" >
<td>
年龄:<asp:TextBox ID="txtAge" runat="server" CssClass ="textBoxStyle"></asp:TextBox>
</td>
</tr>
<tr align ="right" >
<td>
地址:<asp:TextBox ID="txtAddress" runat="server" CssClass ="textBoxStyle"></asp:TextBox>
</td>
</tr>
</table>
我想把xml文件对应的值读取到textbox中,该如何编写代码?
[解决办法]
XMLDocument doc = new XMLDocument()doc.LoadXML(@"<?xml version="1.0" encoding="utf-8" ?><customerInfo><NO1><name>testusd</customerAccount> //你的xml不合法 customerAccount没有开始标记<age>12</customerName><address>testusd</address></NO1><NO2><name>test</customerAccount><age>19</customerName><address>test</address></NO2></customerInfo>");txtName.Text=doc.SelectSingleNode("//NO1/name").InnerText;txtAge.Text=doc.SelectSingleNode("//NO1/age").InnerText;txtAddress.Text=doc.SelectSingleNode("//NO1/address").InnerText;
[解决办法]
XmlDocument doc=new XmlDocument(); doc.Load(Server.MapPath(ConfigurationSettings.AppSettings["xxx"])); //Load XmlNode root=doc.DocumentElement;//根节点 XmlNode opReq=root.SelectSingleNode("xxx"); XmlNode Param=opReq.SelectSingleNode("xxx"); string strName=Param.SelectSingleNode("xxx").value;
[解决办法]
用 XmlDocument
[解决办法]
通用XML读取器 收藏
view plaincopy to clipboardprint?
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//声明XmlReader对象,因为从数据读的时候返回的是这个对象,方便两种方式读取
XmlReader reader = null;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
//此变量用判断是否从数据集读还是文件中读
bool israd = true;
conn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "SELECT [EmployeeID], [City], [Title] FROM [Employees] for xml auto";
//使用三元运算符创建XmlReader对像
reader = israd ? cmd.ExecuteXmlReader() : new XmlTextReader(Request.MapPath("books.xml"));
while (reader.Read())
{
switch (reader.NodeType)
{
//是否是声明
case XmlNodeType.XmlDeclaration:
Response.Write(Server.HtmlEncode("<?xml version='1.0'?>") + "</br>");
break;
//是否是指今
case XmlNodeType.ProcessingInstruction:
Response.Write(Server.HtmlEncode("<?" + reader.Name + " " + reader.Value + "?>") + "</br>");
break;
//是否是元素
case XmlNodeType.Element:
Response.Write(Server.HtmlEncode("<")+reader.Name);
//判断某个元素是否具有属性
if (reader.HasAttributes)
{
//使用循环读出里面的所有属性
for (int i = 0; i < reader.AttributeCount; i++)
{
//使用这个方法依次移动属性
reader.MoveToAttribute(i);
Response.Write(" "+reader.Name+"=" + reader[i]);
}
}
Response.Write(Server.HtmlEncode(">")+"</br>");
break;
//是否是文本
case XmlNodeType.Text:
Response.Write(reader.Value+"</br>");
break;
//是否是注释
case XmlNodeType.Comment:
Response.Write(Server.HtmlEncode("<!--" + reader.Value + "-->") + "</br>");
break;
//是否是结束元素
case XmlNodeType.EndElement:
Response.Write(Server.HtmlEncode("</" + reader.Name + ">") + "</br>");
break;
}
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally
{
//关闭读取器
if (reader != null)
{
reader.Close();
}
//关闭数据库连接
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhlu880516/archive/2008/07/13/2645515.aspx
[解决办法]
标记语言都是对立的,
如:
<name></name>
一样,
区分大小写,
XmlDocument
[解决办法]
用 XmlDocument myXml=new XmlDocument();//声明
myXmL.Load("***.Xml") ;//读取指定文件
XmLNode ***Node=myXml.DocumentElement 定义根节点
foreach(XMLNode node in ***Node.ChildNodes) //遍历子节点
{
switch(node.Name) //获取当前节点的名字<***>
case"":
…………
}
[解决办法]
<?xml version="1.0" encoding="utf-8" ?>
<customerInfo>
<NO1>
<name>testusd</customerAccount>
<age>12</customerName>
<address>testusd</address>
</NO1>
<NO2>
<name>test</customerAccount>
<age>19</customerName>
<address>test</address>
</NO2>
</customerInfo>
最简单的方法把NO1,NO2 都改成 同样的名字,在把xml加载到 datatable 中,就可以了。