ASP如何取XML中<![CDATA[ 中的某个字段的数据,例如FileName的数据。急求答案。 50
<?xmlversion="1.0"encoding="gb2312"?><EpointDataBody><DATA><ReturnInfo><Status>True</...
<?xml version="1.0" encoding="gb2312" ?>
<EpointDataBody>
<DATA><ReturnInfo>
<Status>True</Status>
<Description />
</ReturnInfo><AttachContent><![CDATA[ <AttachContentList><Attach><ContentAttach><FileName>正.doc</FileName></ContentAttach><ContentAttach><ContentType>application/msword</ContentType></ContentAttach><ContentAttach><AttachLength>33808</AttachLength></ContentAttach><ContentAttach><Content>0M8RCA==</Content></ContentAttach><ContentAttach><eeeuid>6211feaa-2399-417f-a28d-42d2a8eab4a</PVIGuid></ContentAttach></Attach></AttachContentList>
]]> </AttachContent>
</DATA>
</EpointDataBody>
以上是1.XML数据源文件,ASP如何取XML中<![CDATA[ 中的某个字段的数据,例如FileName的数据。
我的代码:
<%
set xmldoc=Server.CreateObject("Microsoft.XMLDOM")
xmldoc.load(Server.MapPath("1.xml"))
set objtofind=xmldoc.getElementsByTagName("DATA")
wjjg=objtofind.item(0).text
Response.Write(wjjg)
%>
返回结果是全部的内容,“True 正文.docapplication/msword338080M8RCA==6211feaa-2399-417f-a28d-442d2a8eab4a ”
如何只取XML文件中的FileName或某个字段的信息。 展开
<EpointDataBody>
<DATA><ReturnInfo>
<Status>True</Status>
<Description />
</ReturnInfo><AttachContent><![CDATA[ <AttachContentList><Attach><ContentAttach><FileName>正.doc</FileName></ContentAttach><ContentAttach><ContentType>application/msword</ContentType></ContentAttach><ContentAttach><AttachLength>33808</AttachLength></ContentAttach><ContentAttach><Content>0M8RCA==</Content></ContentAttach><ContentAttach><eeeuid>6211feaa-2399-417f-a28d-42d2a8eab4a</PVIGuid></ContentAttach></Attach></AttachContentList>
]]> </AttachContent>
</DATA>
</EpointDataBody>
以上是1.XML数据源文件,ASP如何取XML中<![CDATA[ 中的某个字段的数据,例如FileName的数据。
我的代码:
<%
set xmldoc=Server.CreateObject("Microsoft.XMLDOM")
xmldoc.load(Server.MapPath("1.xml"))
set objtofind=xmldoc.getElementsByTagName("DATA")
wjjg=objtofind.item(0).text
Response.Write(wjjg)
%>
返回结果是全部的内容,“True 正文.docapplication/msword338080M8RCA==6211feaa-2399-417f-a28d-442d2a8eab4a ”
如何只取XML文件中的FileName或某个字段的信息。 展开
1个回答
展开全部
核心提示:分析ASP.NET读取XML文件4种方法,使用XML控件,使用DOM技术,使用DataSet对象。
方法一 :使用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>
试试
<%@LANGUAGE="JSCRIPT" %>
<%
var objXML=Server.CreateObject("Microsoft.XMLDOM");
objXML.load(Server.MapPath("1.xml"));
var objlst=objXML.getElementsByTagName("属性名称");
intNoOfHeadlines=objLst.length;
response.write(intNoOfHeadlines);
%> 这个吧
方法一 :使用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>
试试
<%@LANGUAGE="JSCRIPT" %>
<%
var objXML=Server.CreateObject("Microsoft.XMLDOM");
objXML.load(Server.MapPath("1.xml"));
var objlst=objXML.getElementsByTagName("属性名称");
intNoOfHeadlines=objLst.length;
response.write(intNoOfHeadlines);
%> 这个吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询