还是那个XML问题

JB-Zhang 2003-08-25 04:03:10
结构如下的XML文件,请问怎么读出它里面的值,如我要读出Name的值"M","千米"等
<xml version="1.0" encoding="GB2312">
<root>
<unit name="M" type="长度" value="1" />
<unit name="米" type="长度" value="1" />
<unit name="公尺" type="长度" value="1" />
<unit name="CM" type="长度" value="0.01" />
<unit name="厘米" type="长度" value = "0.01" />
<unit name="公分" type="长度" value = "0.01" />
<unit name="MM" type="长度" value = "0.001" />
<unit name="毫米" type="长度" value = "0.001" />
<unit name="KM" type="长度" value = "1000" />
<unit name="千米" type="长度" value = "1000" />
<unit name="公里" type="长度" value = "1000" />
<unit name="里" type="长度" value = "500" />
</root>
...全文
69 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
panyee 2003-08-26
  • 打赏
  • 举报
回复
你的xml写错了
<?xml version="1.0"?>

用下面的代码:

string str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root><unit name=\"M\" type=\"长度\" value=\"1\" />";
str += "<unit name=\"米\" type=\"长度\" value=\"1\" /></root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
MessageBox.Show(doc.OuterXml);
XmlNodeList nodes = doc.DocumentElement.SelectNodes("unit");
foreach(XmlNode node in nodes)
{
string strTemp = node.Attributes["name"].Value + ":" + node.Attributes["type"].Value + ":" + node.Attributes["value"].Value ;
MessageBox.Show(strTemp);
}
Aaron_Lau 2003-08-25
  • 打赏
  • 举报
回复
XPathDocument xpath = new XPathDocument(xmlfile) ;
XPathNavigator nav = xpath.CreateNavigator() ;
XPathNodeIterator iterator = nav.Select("root/unit/@name") ;
ArrayList al = new ArrayList() ;
while(iterator.MoveNext())
{
al.Add(iterator.Current.Value) ;
}
这样你就把你想要的值存到al中的。然后用的时候你可以将值取出来。
JB-Zhang 2003-08-25
  • 打赏
  • 举报
回复
各位DX,快帮帮忙,急死了,交不了差呀!
JB-Zhang 2003-08-25
  • 打赏
  • 举报
回复
是的,用你的代码,只能读出"unit",无法读出值,如"平方米","公里"之类的
firejie 2003-08-25
  • 打赏
  • 举报
回复
......你确定你的文件没有给错?
firejie 2003-08-25
  • 打赏
  • 举报
回复
if(reader.NodeType==XmlNodeType.XmlDeclaration)
{
richTextBox1.AppendText("<?xml version='1.0' encoding='utf-8'?>"+"\n");
}


if(reader.NodeType==XmlNodeType.Element)
{
richTextBox1.AppendText("<"+reader.Name+">");
reader.Read();
}

if(reader.NodeType==XmlNodeType.Element)
{
richTextBox1.AppendText("\n"+"<"+reader.Name+">");
reader.Read();
}


if(reader.NodeType==XmlNodeType.Element)
richTextBox1.AppendText("\n"+"<"+reader.Name+">");
else if(reader.NodeType==XmlNodeType.Text)
richTextBox1.AppendText(reader.Value);
else if(reader.NodeType==XmlNodeType.EndElement) richTextBox1.AppendText("</"+reader.Name+">"+"\n");
JB-Zhang 2003-08-25
  • 打赏
  • 举报
回复
怎么没人帮忙?急呀,快点!!分不够再加

110,533

社区成员

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

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

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