首页 > 开发 > XML > 正文

如何读取XML文件内容

2024-09-05 20:56:00
字体:
来源:转载
供稿:网友

下面是通过读取xml文件中的内容并显示在textbox的例子:


using system;
using system.data;
using system.configuration;
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.xml.xpath;

public partial class _default : system.web.ui.page
{
    public string[] x = new string[4];
    protected void page_load(object sender, eventargs e)
    {

        xmldocument xmldoc = new xmldocument();
        xmldoc.load(server.mappath("table.xml"));//你的xml文件
        xmlnodelist xmllist = xmldoc.selectsinglenode("mapsheet").childnodes;
        foreach (xmlnode xmlno in xmllist)
        {
            //xmlnodelist xe = (xmlnodelist)xmlno;
            xmlelement xe = (xmlelement)xmlno;
            {
                if (xe.name == "tablename")
                {
                    y[0] = xe.innertext;
                }

                if (xe.name == "datasource")
                {
                    y[1] = xe.innertext;
                }

                if (xe.name == "username")
                {
                    y[2] = xe.innertext;
                }

                if (xe.name == "password")
                {
                    y[3] = xe.innertext;
                }
            }
        }
        textbox1.text=y[0];
        textbox2.text = y[1];
        textbox3.text = y[2];
        textbox4.text = y[3];
    }

}该程序已经过测试,没有问题。

菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表