目前XML
<?xml version="1.0" ?>
- <Configuration>
- <RelationshipCCIp>
<ccValue>10.137.144.81:21:123:456</ccValue>
<ccValue>10.137.144.82:21:123:456</ccValue>
<ccValue>10.137.144.83:21:123:456</ccValue>
<ccValue>10.137.144.84:21:123:456</ccValue>
<ccValue>10.137.144.85:21:123:456</ccValue>
<ccValue>10.137.144.86:21:123:456</ccValue>
<ccValue>10.137.144.87:21:123:456</ccValue>
<ccValue>10.137.144.88:21:123:456</ccValue>
</RelationshipCCIp>
<Configuration>
最终我想把ccValue中的所有数据都提出来?我这样写只能获取第一个?
List<string> rstInfo = new List<string>();
string cfgPath = @"d:\Config.xml";
XmlDocument doc = new XmlDocument();
try
{
doc.Load(cfgPath);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
} try
{
foreach (XmlNode group in doc.SelectNodes(@"Configuration/RelationshipCCIp"))
{
string info = null;
XmlNode item;
item = group.SelectSingleNode("ccValue");
info = item.InnerText;
rstInfo.Add(info);
} }
catch(Exception e)
{
MessageBox.Show(e.Message);
}
<?xml version="1.0" ?>
- <Configuration>
- <RelationshipCCIp>
<ccValue>10.137.144.81:21:123:456</ccValue>
<ccValue>10.137.144.82:21:123:456</ccValue>
<ccValue>10.137.144.83:21:123:456</ccValue>
<ccValue>10.137.144.84:21:123:456</ccValue>
<ccValue>10.137.144.85:21:123:456</ccValue>
<ccValue>10.137.144.86:21:123:456</ccValue>
<ccValue>10.137.144.87:21:123:456</ccValue>
<ccValue>10.137.144.88:21:123:456</ccValue>
</RelationshipCCIp>
<Configuration>
最终我想把ccValue中的所有数据都提出来?我这样写只能获取第一个?
List<string> rstInfo = new List<string>();
string cfgPath = @"d:\Config.xml";
XmlDocument doc = new XmlDocument();
try
{
doc.Load(cfgPath);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
} try
{
foreach (XmlNode group in doc.SelectNodes(@"Configuration/RelationshipCCIp"))
{
string info = null;
XmlNode item;
item = group.SelectSingleNode("ccValue");
info = item.InnerText;
rstInfo.Add(info);
} }
catch(Exception e)
{
MessageBox.Show(e.Message);
}
foreach(XElement xe in doc.Root.Element("RelationshipCCIp").Elements("ccValue"))
{
string info = xe.Text;//"10.137.144.81:21:123:456"
}循环遍历,请加using System.Xml.Linq;