ASP.NET中读取XML文件信息的4种方法与示例代码
程序员文章站
2023-11-27 12:29:34
方法一 :使用xml控件 <% @ page language="c#"%>
方法一 :使用xml控件
<% @ page language="c#"%>
<html>
<body>
<h3><font face="verdana">读取xml方法一</font></h3>
<from runat=server>
<asp:xml id="xml1" documentsource="grade.xml" runat="server" />
</from>
</body>
</html>
方法二: 使用dom技术
<% @ page language="c#"%>
<% @ import namespace="system.xml"%>
<% @ import namespace="system.xml.xsl"%>
<html>
<script language="c#" runat="server">
void page_load(object sender,eventargs e)
{
xmldocument doc=new xmldocument();
doc.load(server.mappath("grade.xml"));
xml1.document=doc;
}
</script>
<body>
<h3><font face="verdana">读取xml方法二</font></h3>
<from runat=server>
<asp:xml id="xml1" runat="server" />
</from>
</body>
</html>
方法三 :使用dataset对象
<% @ page language="c#"%>
<% @ import namespace="system.data"%>
<% @ import namespace="system.data.oledb"%>
<script language="c#" runat="server">
void page_load(object sender,eventargs e)
{
dataset objdataset=new dataset();
objdataset.readxml(server.mappath("grade.xml"));
dgemployees.datasource=objdataset.tables["student"].defaultview;
dgemployees.databind();
}
</script>
<body>
<h3><font face="verdana">读取xml方法三</font></h3>
<asp:datagrid id="dgemployees" runat="server" />
</body>
</html>
方法四 :按文本方式读取
<% @ page language="c#"%>
<% @ import namespace="system.xml"%>
<html>
<script language="c#" runat="server">
private void page_load(object sender,eventargs e)
{
xmltextreader objxmlreader=new xmltextreader(server.mappath("grade.xml"));
string strnoderesult="";
xmlnodetype objnodetype;
while(objxmlreader.read())
{
objnodetype =objxmlreader.nodetype;
swith(objnodetype)
{
case xmlnodetype.xmldeclaration:
//读取xml文件头
strnoderesult+="xml declaration:<b>"+objxmlreader.name+""+objxmlreader.value+"</b><br/>";
break;
case xmlnodetype.element:
//读取标签
strnoderesult+="element:<b>"+objxmlreader.name+"</b><br/>";
break;
case xmlnodetype.text:
//读取值
strnoderesult+=" -value:<b>"+objxmlreader.value+"</b><br/>";
break;
}
//判断该节点是否有属性
if(objxmlreader.attributecount>0)
{ //用循环判断完所有节点
while(objxmlreader.movetonextattibute)
{ //取标签和值
strnoderesult+=" -attribute:<b>"+objxmlreader.name+"</b> value:<b>"+objxmlreader.value+"</b><br/>";
}
}
lblfile.text=strnoderesult;
}
}
</script>
<body>
<h3><font face="verdana">读取xml方法四</font></h3>
<from runat=server>
<asp:label id="lblfile" runat="server" />
</from>
</body>
</html>
<% @ page language="c#"%>
<html>
<body>
<h3><font face="verdana">读取xml方法一</font></h3>
<from runat=server>
<asp:xml id="xml1" documentsource="grade.xml" runat="server" />
</from>
</body>
</html>
方法二: 使用dom技术
<% @ page language="c#"%>
<% @ import namespace="system.xml"%>
<% @ import namespace="system.xml.xsl"%>
<html>
<script language="c#" runat="server">
void page_load(object sender,eventargs e)
{
xmldocument doc=new xmldocument();
doc.load(server.mappath("grade.xml"));
xml1.document=doc;
}
</script>
<body>
<h3><font face="verdana">读取xml方法二</font></h3>
<from runat=server>
<asp:xml id="xml1" runat="server" />
</from>
</body>
</html>
方法三 :使用dataset对象
<% @ page language="c#"%>
<% @ import namespace="system.data"%>
<% @ import namespace="system.data.oledb"%>
<script language="c#" runat="server">
void page_load(object sender,eventargs e)
{
dataset objdataset=new dataset();
objdataset.readxml(server.mappath("grade.xml"));
dgemployees.datasource=objdataset.tables["student"].defaultview;
dgemployees.databind();
}
</script>
<body>
<h3><font face="verdana">读取xml方法三</font></h3>
<asp:datagrid id="dgemployees" runat="server" />
</body>
</html>
方法四 :按文本方式读取
<% @ page language="c#"%>
<% @ import namespace="system.xml"%>
<html>
<script language="c#" runat="server">
private void page_load(object sender,eventargs e)
{
xmltextreader objxmlreader=new xmltextreader(server.mappath("grade.xml"));
string strnoderesult="";
xmlnodetype objnodetype;
while(objxmlreader.read())
{
objnodetype =objxmlreader.nodetype;
swith(objnodetype)
{
case xmlnodetype.xmldeclaration:
//读取xml文件头
strnoderesult+="xml declaration:<b>"+objxmlreader.name+""+objxmlreader.value+"</b><br/>";
break;
case xmlnodetype.element:
//读取标签
strnoderesult+="element:<b>"+objxmlreader.name+"</b><br/>";
break;
case xmlnodetype.text:
//读取值
strnoderesult+=" -value:<b>"+objxmlreader.value+"</b><br/>";
break;
}
//判断该节点是否有属性
if(objxmlreader.attributecount>0)
{ //用循环判断完所有节点
while(objxmlreader.movetonextattibute)
{ //取标签和值
strnoderesult+=" -attribute:<b>"+objxmlreader.name+"</b> value:<b>"+objxmlreader.value+"</b><br/>";
}
}
lblfile.text=strnoderesult;
}
}
</script>
<body>
<h3><font face="verdana">读取xml方法四</font></h3>
<from runat=server>
<asp:label id="lblfile" runat="server" />
</from>
</body>
</html>
上一篇: 推荐11个实用Python库