怎么用C#取节点值

szliukai 2004-10-30 12:22:45
<root>
<item>
<partno>lk1</partno>
<techno>1211113</techno>
<techname>fsdafsd</techname>
</item>
<item>
<partno>lk2</partno>
<techno>1231113</techno>
<techname>fdsdafsd</techname>
</item>
<item>
<partno>lk3</partno>
<techno>1241113</techno>
<techname>fsdafsd</techname>
</item>
<item>
<partno>lk6</partno>
<techno>121d113</techno>
<techname>fsdafsd</techname>
</item>
</root>

这个xml怎么提取每个item节点下的子节点值!!
我要单项的取,因为我要单个的加入到数据集中,比如
lcpartno=(取节点partno的值)
lctechno=(取节点techno的值)
lctechname=(取节点techname的值)
...全文
114 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
BearRui 2004-10-30
  • 打赏
  • 举报
回复
找到所有partno的值:
---------------------------
XmlDocument doc=new XmlDocument();
doc.Load("filename");
XmlNodeList nodes=doc.SelectNodes("//partno");

foreach(XmlNode node in nodes)
Console.WriteLine(node.InnerText);
boyxia 2004-10-30
  • 打赏
  • 举报
回复
//读节点1----------------------------------------------------------------
// XmlTextReader tr=new XmlTextReader("books.xml");
// while(tr.Read())
// {
// if(tr.NodeType==XmlNodeType.Text)
// listBox1.Items.Add(tr.Value);
// }
//----------------------------------------------------------------------
//读属性
FileStream fs=new FileStream("books.xml",FileMode.Open);
XmlTextReader tr=new XmlTextReader(fs);
while(tr.Read())
{
if(tr.MoveToContent()==XmlNodeType.Element)
{

for(int i=0;i<tr.AttributeCount;i++)
{
listBox1.Items.Add(tr.GetAttribute(i));
}
}

}
//读节点2----------------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
FileStream fs=new FileStream("books.xml",FileMode.Open);
XmlTextReader tr=new XmlTextReader(fs);
while(!tr.EOF)
{
if(tr.MoveToContent()==XmlNodeType.Element&&tr.Name=="title")

{
//从元素中读取文本数据
//listBox1.Items.Add(tr.ReadElementString());//自动定位到下一个节点
LoadList(tr);//忽略异常

}
else
{
tr.Read();
}
}
}
private void LoadList(XmlReader reader)
{
try
{
listBox1.Items.Add(reader.ReadElementString());//自动定位到下一个节点
}
catch(XmlException er){}//ignore

}
//----------------------------------------------------------------------

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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