日期:2014-05-18  浏览次数:20796 次

c# 操作XML文件问题
有以下的XML文件
<?xml version="1.0" encoding="ISO-8859-1"?>

<catalog>

  <cd country="USA">

  <title>Empire Burlesque</title>

  <artist>Bob Dylan</artist>

  <price>10.90</price>

  </cd>

  <cd country="UK">

  <title>Hide your heart</title>

  <artist>Bonnie Tyler</artist>

  <price>9.90</price>

  </cd>

  <cd country="USA">

  <title>Greatest Hits</title> 

  <artist>Dolly Parton</artist> 

  <price>9.90</price> 

  </cd>

</catalog>



如何分别按顺序选出<title>和<artist>中的内容?谢谢!

------解决方案--------------------


XmlDocument doc = new XmlDocument();
doc.Load("c:\\a.xml");
foreach (XmlNode catalogNode in doc.ChildNodes)
{
if (catalogNode.Name == "catalog")
{
foreach (XmlNode cdNode in catalogNode.ChildNodes)
{
foreach (XmlNode node in cdNode.ChildNodes)
{
if (node.Name == "title")
{
Console.WriteLine("Title:{0}", node.InnerText);
}
else if (node.Name == "artist")
{
Console.WriteLine("Artist:{0}", node.InnerText);
}
}
}
}
}
------解决方案--------------------
private void ReadXml()
{
ArrayList ArryList11 = new ArrayList();
ArrayList ArryList12 = new ArrayList();

//
string xmlPath = Server.MapPath("xxxxx.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);

XmlNodeList xnl = xmlDoc.SelectSingleNode("catalog").ChildNodes;


foreach (XmlNode node in xnl)
{
foreach (XmlNode titlenode in node.SelectNodes("title"))
{
ArryList11.Add(titlenode.InnerText);
}
foreach (XmlNode artistnode in node.SelectNodes("artist"))
{
ArryList12.Add(artistnode.InnerText);
}
}
}