百分在线等,读取xml内容

sljz 2007-07-10 04:44:03
Sorry各位,刚才发的那个帖子可能说的不清楚,现在时间紧迫,因此重新叙述一下。

现在通过其他程序,获取了一个xml(注意,该xml格式不能修改),存入了一个xmlreader。

__________________________________________________
XML如下:

<product>
<name>产品1</name>
<info>介绍1</info>
<details>
<detail>
<color>黄色</color>
<height>100</height>
</detail>
<detail>
<color>蓝色</color>
<height>200</height>
</detail>
</details>
</product>

------------------------------------------------------------
现在要求页面上按如下格式显示:

产品名为:产品1
产品介绍:介绍1
产品特性一共有:2 种

产品特性1的颜色为:黄色
产品特性1的高度为:100

产品特性1的颜色为:蓝色
产品特性1的高度为:200


------------------------------------------------------------

备注:
1. XML格式无法修改,现在能获得的就一个xmlreader
2. 请老大写出详细的代码(本人菜鸟,敬请谅解,万勿省略)
...全文
249 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuhuiITren 2007-07-11
  • 打赏
  • 举报
回复
顶一下
job_2006 2007-07-11
  • 打赏
  • 举报
回复
顶一下
dracula1133 2007-07-11
  • 打赏
  • 举报
回复
System.Xml.XmlDocument dom = new System.Xml.XmlDocument();
dom.LoadXml(strXML);
System.Xml.XmlNodeList nl = dom.SelectNodes("//product");
foreach (System.Xml.XmlNode node in nl)
{
Response.Write("name:" + node.ChildNodes[0].InnerText + "<BR>");
Response.Write("info:" + node.ChildNodes[1].InnerText + "<BR>");
int iCount = node.ChildNodes[2].ChildNodes.Count;
Response.Write("details:" + iCount.ToString() + "<BR>");
for (int i = 0; i < iCount; i++)
{
Response.Write("p" + i + " COLOR:" + node.ChildNodes[2].ChildNodes[i].ChildNodes[0].InnerText + "<BR>");
Response.Write("p" + i + " height:" + node.ChildNodes[2].ChildNodes[i].ChildNodes[1].InnerText + "<BR>");
}
}
hui1202 2007-07-11
  • 打赏
  • 举报
回复
难不成楼主是小姑娘?
xeppp 2007-07-10
  • 打赏
  • 举报
回复
争先恐后啊  呵呵  有点意思
debug1984 2007-07-10
  • 打赏
  • 举报
回复
oh,帮顶下~
可爱的排骨 2007-07-10
  • 打赏
  • 举报
回复
还差我的答案
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:apply-templates select="product" />
</xsl:template>

<xsl:template match="product">
产品名为:<xsl:value-of select="name" /><br />
产品介绍:<xsl:value-of select="info" /><br />
产品特性一共有:<xsl:value-of select="count(details/detail)" /> 种<br />
<xsl:apply-templates select="details/detail" />
</xsl:template>

<xsl:template match="details/detail">
<br />
产品特性1的颜色为:<xsl:value-of select="color" /><br />
产品特性1的高度为:<xsl:value-of select="height" /><br />
</xsl:template>
</xsl:stylesheet>

Jinglecat 2007-07-10
  • 打赏
  • 举报
回复
非得用 XmlReader ?填饱肚皮回来给你代码
wheatlee 2007-07-10
  • 打赏
  • 举报
回复
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>XML演示</title>
<script language="javascript" for="windows" event ="onload">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML("test.xml");
nodes=xmlDoc.documentElement.childNodes;
productName.innerText += nodes.item(0).text;
productIntroduce.innerText += nodes.item(1).text;
productNum.innerText += nodes.item(2).childNodes.length;

productColor1.innerText += nodes.item(2).childNodes.item(0).childNodes.item(0).text;
productHeight1.innerText += nodes.item(2).childNodes.item(0).childNodes.item(1).text;

productColor2.innerText += nodes.item(2).childNodes.item(1).childNodes.item(0).text;
productHeight2.innerText += nodes.item(2).childNodes.item(1).childNodes.item(1).text;

</script>
</head>
<body>
<h3><font face="Verdana" runat="server">XML演示<span runat="server" id = "MySpan"></span></font></h3>
<asp:DataGrid runat="server" ID="MyGrid" ></asp:DataGrid>

<span id="productName">产品名</span>
<br />
<span id="productIntroduce">产品介绍</span>
<br />
<span id="productNum">产品特性一共有:</span>
<br />
<br />
<span id="productColor1">产品特性1的颜色为:</span>
<br />
<span id="productHeight1">产品特性1的高度是:</span>
<br />
<span id="productColor2">产品特性2的颜色为:</span>
<br />
<span id="productHeight2">产品特性2的高度是:</span>
</body>
</html>
cancerser 2007-07-10
  • 打赏
  • 举报
回复
DataSet ds=new DataSet();
ds.ReadXml("test.xml");
DataTable dtP=ds.Tables["product"];
DataTable dtD=ds.Tables["detail"];//为特性表
string 产品名为=dtP.Rows[0][0].ToString();
string 产品介绍=dtP.Rows[0][1].ToString();
int 产品特性一共有=dtD.Rows.Count;
//本人最喜欢的彪悍性写法,读的时候可能慢,笑笑得了
SassyBoy 2007-07-10
  • 打赏
  • 举报
回复
string xml =
"<product>"
+ "<name>产品1</name>"
+ "<info>介绍1</info>"
+ "<details>"
+ "<detail>"
+ "<color>黄色</color>"
+ "<height>100</height>"
+ "</detail>"
+ "<detail>"
+ "<color>蓝色</color>"
+ "<height>200</height>"
+ "</detail>"
+ "</details>"
+ "</product>";

XmlDocument xd = new XmlDocument();
xd.LoadXml(xml);

XmlNode xn_name = xd.SelectSingleNode("product/name");
XmlNode xn_info = xd.SelectSingleNode("product/info");
XmlNodeList xn_details = xd.SelectNodes("product/details/detail");

Response.Write(xn_name.InnerText+"<br>");
Response.Write(xn_info.InnerText+"<br>");

for (int i = 0; i < xn_details.Count; i++)
{
Response.Write(
xn_details[i].ChildNodes[0].Name + "--"
+ xn_details[i].ChildNodes[0].InnerText + "<br>"
+ xn_details[i].ChildNodes[1].Name + "--"
+ xn_details[i].ChildNodes[1].InnerText+"<br>"
);
}
cpp2017 2007-07-10
  • 打赏
  • 举报
回复
string strXML = @"<?xml version=""1.0"" encoding=""GB2312"" ?> <product> <name>产品1</name> <info>介绍1</info> <details> <detail> <color>黄色</color> <height>100</height> </detail> <detail> <color>蓝色</color> <height>200</height> </detail> </details> </product> "; System.Xml.XmlDocument dom = new System.Xml.XmlDocument(); dom.LoadXml(strXML); System.Xml.XmlNodeList nl = dom.SelectNodes("//product"); foreach(System.Xml.XmlNode node in nl) { Response.Write("name:" + node.ChildNodes[0].InnerText + "<BR>"); Response.Write("info:" + node.ChildNodes[1].InnerText + "<BR>"); Response.Write("details:" + node.ChildNodes[2].ChildNodes.Count.ToString() + "<BR>"); Response.Write("p1 COLOR:" + node.ChildNodes[2].ChildNodes[0].ChildNodes[0].InnerText+ "<BR>"); Response.Write("p1 height:" + node.ChildNodes[2].ChildNodes[0].ChildNodes[1].InnerText + "<BR>"); Response.Write("p2 COLOR:" + node.ChildNodes[2].ChildNodes[1].ChildNodes[0].InnerText + "<BR>"); Response.Write("p2 height:" + node.ChildNodes[2].ChildNodes[1].ChildNodes[1].InnerText + "<BR>"); }
sljz 2007-07-10
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="GB2312" ?>
<product>
<name>产品1</name>
<info>介绍1</info>
<details>
<detail>
<color>黄色</color>
<height>100</height>
</detail>
<detail>
<color>蓝色</color>
<height>200</height>
</detail>
</details>
</product>


整个XML就这么样的
cancerser 2007-07-10
  • 打赏
  • 举报
回复
大哥 你把xml头贴出来成不
sljz 2007-07-10
  • 打赏
  • 举报
回复
晕,显示格式最后数字写错了

------------------------------------------------------------
现在要求页面上按如下格式显示:

产品名为:产品1
产品介绍:介绍1
产品特性一共有:2 种

产品特性1的颜色为:黄色
产品特性1的高度为:100

产品特性2的颜色为:蓝色
产品特性2的高度为:200


------------------------------------------------------------

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧