读取xml

lwz1983 2008-08-07 12:02:12
<Employees>
<NO1>
<UserName></UserName>
<UserPhone></UserPhone>
<MailAddress></MailAddress>
<Email></Email>
<DeviceID>yy</DeviceID>
<Vendors></Vendors>
<VendorsrPhone>
</VendorsrPhone>
<Experience>as</Experience>
<BuyDate>2008-08-07 </BuyDate>
</NO1>
</Employees>

我想要的读取到的值为str=";;;;yy;;;as;2008-08-07"就是读取NO1节点下所有节点的text(即使为空)
...全文
102 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangrui6200824 2011-12-29
  • 打赏
  • 举报
回复
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"D:\Project\C#Test\WindowsApplication1\WindowsApplication4\XMLFile1.xml");

XmlNode node = xmlDoc.SelectSingleNode("/Employees/NO1");
string str="";
Foreach(XmlNode n in node.ChildNodes)
{
str=str+n.InnerText+";";

二楼正解!!!建议楼主结贴了
virusswb 2008-08-07
  • 打赏
  • 举报
回复
;;;;;yy ;;;as ;2008-08-07

这就是结果
virusswb 2008-08-07
  • 打赏
  • 举报
回复
测试通过

private void button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
StringReader sr = new StringReader(richTextBox1.Text);
XPathDocument doc = new XPathDocument(sr);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = nav.Select("/Employees/NO1");
while (iter.MoveNext())
{
XPathNodeIterator xpthIter = iter.Current.SelectChildren(XPathNodeType.Element);
while (xpthIter.MoveNext())
{
richTextBox2.AppendText(";"+xpthIter.Current.ToString());
}

}
}
virusswb 2008-08-07
  • 打赏
  • 举报
回复

private void button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
StringReader sr = new StringReader(richTextBox1.Text);
XPathDocument doc = new XPathDocument(sr);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = nav.Select("/Employees/NO1");
while (iter.MoveNext())
{
XPathNodeIterator xpthIter = iter.Current.SelectChildren(XPathNodeType.Element);
while (xpthIter.MoveNext())
{
richTextBox2.AppendText(";"+xpthIter.Current.ToString());
}

}
}
yagebu1983 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ericzhangbo1982111 的回复:]
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"D:\Project\C#Test\WindowsApplication1\WindowsApplication4\XMLFile1.xml");

XmlNode node = xmlDoc.SelectSingleNode("/Employees/NO1");
string str="";
Foreach(XmlNode n in node.ChildNodes)
{
str=str+n.InnerText+";";
}
[/Quote]
同意!!
BillMhw 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ericzhangbo1982111 的回复:]
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"D:\Project\C#Test\WindowsApplication1\WindowsApplication4\XMLFile1.xml");

XmlNode node = xmlDoc.SelectSingleNode("/Employees/NO1");
string str="";
Foreach(XmlNode n in node.ChildNodes)
{
str=str+n.InnerText+";";
}
[/Quote]
zzyhuian06142 2008-08-07
  • 打赏
  • 举报
回复
XmlDocument doc= new XmlDpcument();
doc.load(XML路径);
XmlNodeList nodes =doc.GetNodesByTag("NO1");
foreach(XmlNode n in nodes)
{
string str ="";
str +=node.ChildNodes.Item(0).InnerText;
str +=node.ChildNodes.Item(1).InnerText;
str +=node.ChildNodes.Item(2).InnerText;
str +=node.ChildNodes.Item(3).InnerText;
str +=node.ChildNodes.Item(4).InnerText;
str +=node.ChildNodes.Item(5).InnerText;
str +=node.ChildNodes.Item(6).InnerText;
str +=node.ChildNodes.Item(7).InnerText;
str +=node.ChildNodes.Item(8).InnerText;

}
ericzhangbo1982111 2008-08-07
  • 打赏
  • 举报
回复
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"D:\Project\C#Test\WindowsApplication1\WindowsApplication4\XMLFile1.xml");

XmlNode node = xmlDoc.SelectSingleNode("/Employees/NO1");
string str="";
Foreach(XmlNode n in node.ChildNodes)
{
str=str+n.InnerText+";";
}
巨巨 2008-08-07
  • 打赏
  • 举报
回复
XmlTextReader reader=new XmlTextReader("你的文件名");

string key = "";

try
{
while(reader.Read())
{
if (reader.NodeType==XmlNodeType.Element)
{
if (reader.Name=="UserName")
{
key = reader.ReadElementString().Trim();
info.Add(key, "");
}
....
}
}
}
catch(System.Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
if (reader!=null)
{
reader.Close();
}
}
大致就是这个框架吧

110,534

社区成员

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

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

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