日期:2014-05-20  浏览次数:20829 次

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中,该如何编写代码?

------解决方案--------------------
C# code
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;

------解决方案--------------------
C# code

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对象,因为从数据读的时候返回的是这个对象,方便两种方式读取