读取xml
<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(即使为空)
------解决方案--------------------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();
}
}
大致就是这个框架吧
------解决方案-------------------- 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+";";
}
------解决方案--------------------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;
}