首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

DataGrid 绑定 xml 的有关问题

2012-01-07 
DataGrid 绑定 xml 的问题XmlDocumentdomnewXmlDocument()dom.LoadXml(usersxml)dom.Save( d:/userxml

DataGrid 绑定 xml 的问题
XmlDocument   dom   =   new   XmlDocument();
dom.LoadXml(usersxml);
dom.Save( "d:/userxml.xml ");
dom.RemoveAll();
DataSet   ds   =new   DataSet();
ds.ReadXml( "d:/userxml.xml ");
this.DataGrid1.DataSource   =   ds.Tables[0];
this.DataGrid1.DataBind();

什么都没读出来

其实只是想取得   一列就行     每个 <user> 里的 <param   name= "realname "   value= "陈立军 "   /> 这个

也换了种写法     可也没显示出   数据     求XDJM们帮帮忙看下~~~谢谢  


XmlNodeList     nodes   =   dom.SelectNodes( "orgUnit/user/param[@name= 'realname '] ");
if(nodes!=null)
{
  foreach   (XmlNode   node   in   nodes)
  {
    DataTable   dt   =   new   DataTable();
    dt.Columns.Add( "name ",Type.GetType( "System.String "));

    dt.Columns.Add(node.Attributes[ "realname "].Value);
    this.DataGrid1.DataSource=dt;
    this.DataGrid1.DataBind();
}


---------------------
上面的程序也   显示不出数据~~~

下面是我的XML格式及内容

<?xml   version= "1.0 "   encoding= "UTF-8 "?>
<orgUnit   id= "2 "   name= "办公室 ">
    <user>
        <param   name= "category "   value= " "   />
        <param   name= "createdate "   value= "2007-3-28 "   />
        <param   name= "email "   value= "clj@jsppd.gov.cn "   />
        <param   name= "mobile "   value= "1234567890 "   />
        <param   name= "orgid "   value= "2 "   />
        <param   name= "orgname "   value= " "   />
        <param   name= "otherway "   value= "1234567890 "   />
        <param   name= "password "   value= "666 "   />
        <param   name= "phone "   value= " "   />
        <param   name= "position "   value= "主任 "   />
        <param   name= "realname "   value= "陈立军 "   />
        <param   name= "registerdate "   value= "2007-3-28 "   />
        <param   name= "servicedate "   value= "2018-10-11 "   />
        <param   name= "status "   value= "N "   />
        <param   name= "type "   value= "01 "   />
        <param   name= "userdsc "   value= "陈立军 "   />
        <param   name= "userid "   value= "135 "   />
        <param   name= "username "   value= "clj@jsppd.gov.cn "   />
    </user>    
    <user>


        <param   name= "category "   value= " "   />
        <param   name= "createdate "   value= "2007-3-28 "   />
        <param   name= "email "   value= "zjj2@jsppd.gov.cn "   />
        <param   name= "mobile "   value= " "   />
        <param   name= "orgid "   value= "2 "   />
        <param   name= "orgname "   value= " "   />
        <param   name= "otherway "   value= " "   />
        <param   name= "password "   value= "666 "   />
        <param   name= "phone "   value= " "   />
        <param   name= "position "   value= " "   />
        <param   name= "realname "   value= "张建洁 "   />
        <param   name= "registerdate "   value= "2007-3-28 "   />
        <param   name= "servicedate "   value= "2018-10-11 "   />
        <param   name= "status "   value= "N "   />
        <param   name= "type "   value= "01 "   />
        <param   name= "userdsc "   value= "张建洁 "   />
        <param   name= "userid "   value= "28 "   />
        <param   name= "username "   value= "zjj2@jsppd.gov.cn "   />
    </user>
    <user>
        <param   name= "category "   value= " "   />
        <param   name= "createdate "   value= "2007-3-28 "   />
        <param   name= "email "   value= "zw@jsppd.gov.cn "   />
        <param   name= "mobile "   value= " "   />
        <param   name= "orgid "   value= "2 "   />
        <param   name= "orgname "   value= " "   />
        <param   name= "otherway "   value= " "   />
        <param   name= "password "   value= "666 "   />
        <param   name= "phone "   value= " "   />
        <param   name= "position "   value= " "   />
        <param   name= "realname "   value= "周伟 "   />
        <param   name= "registerdate "   value= "2007-3-28 "   />
        <param   name= "servicedate "   value= "2018-10-11 "   />


        <param   name= "status "   value= "N "   />
        <param   name= "type "   value= "01 "   />
        <param   name= "userdsc "   value= "周伟 "   />
        <param   name= "userid "   value= "29 "   />
        <param   name= "username "   value= "zw@jsppd.gov.cn "   />
    </user>
    <user>
        <param   name= "category "   value= " "   />
        <param   name= "createdate "   value= "2007-3-28 "   />
        <param   name= "email "   value= "zxm@jsppd.gov.cn "   />
        <param   name= "mobile "   value= " "   />
        <param   name= "orgid "   value= "2 "   />
        <param   name= "orgname "   value= " "   />
        <param   name= "otherway "   value= " "   />
        <param   name= "password "   value= "666 "   />
        <param   name= "phone "   value= " "   />
        <param   name= "position "   value= " "   />
        <param   name= "realname "   value= "张笑梅 "   />
        <param   name= "registerdate "   value= "2007-3-28 "   />
        <param   name= "servicedate "   value= "2018-10-11 "   />
        <param   name= "status "   value= "N "   />
        <param   name= "type "   value= "01 "   />
        <param   name= "userdsc "   value= "张笑梅 "   />
        <param   name= "userid "   value= "30 "   />
        <param   name= "username "   value= "zxm@jsppd.gov.cn "   />
    </user>
</orgUnit>


分不够再加     请帮帮忙~~~

[解决办法]
this.DataGrid1.DataSource = ds.Tables[0].DateView;

[解决办法]
你取出XML已经到DataSet中了,可以直接用DataTable的Select方法查找出你要的数据,再绑定不就行了。
[解决办法]
第二种的写法不对,应该是:

XmlNodeList nodes = dom.SelectNodes( "orgUnit/user/param[@name= 'realname '] ");
if (nodes != null)
{
DataTable dt = new DataTable();
dt.Columns.Add( "name ", Type.GetType( "System.String "));
foreach (XmlNode node in nodes)
{
DataRow row = dt.NewRow();
row[ "name "] = node.Attributes[ "value "].Value;


dt.Rows.Add(row);

}
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}

热点排行