ServerConfig.xml
<root>
<Server id="1">
<IP>localhost</IP>
<port>80</port>
</Server>
<Server id="2">
<IP>127.0.0.1</IP>
<port>8080</port>
</Server>
</root>
XmlTest2.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
namespace XmlTest
{
public class XmlReader
{
static void Main()
{
string ip;
int port;
string xmlFile = "ServerConfig.xml";
XmlDocument xmlDoc = new XmlDocument();
string xpath = "/root/Server";
try
{
xmlDoc.Load(xmlFile);
XmlNodeList nodeList = xmlDoc.SelectNodes(xpath);
if (nodeList != null)
{
foreach (XmlNode aNode in nodeList)
{
Console.WriteLine(aNode.OuterXml);
ip = (aNode.SelectSingleNode("IP")).InnerText;
Console.WriteLine(ip);
port = int.Parse((aNode.SelectSingleNode("port")).InnerText);
Console.WriteLine(port.ToString());
}
}
else
{
Console.WriteLine(" nodeList is null.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}