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

XML選值的問題
XML code

<?xml version="1.0" standalone="yes" ?> 
<TestCase>
  <Case CaseID="Case001">
      <User UserID="User001">
          <FunctionName>FName1</FunctionName>
          <ActType>Create</ActType>
          <RequestNumber>3</RequestNumber>
          <FrequencyNumber>2</FrequencyNumber>
          <DelayNumber>1</DelayNumber>
      </User>
      <User UserID="User002">
          <FunctionName>FName2</FunctionName>
          <ActType>Update</ActType>
          <RequestNumber>5</RequestNumber>
          <FrequencyNumber>2</FrequencyNumber>
          <DelayNumber>3</DelayNumber>
      </User>      
  </Case>
  <Case CaseID="Case002">
      <User UserID="User001">
          <FunctionName>FName21</FunctionName>
          <ActType>Create</ActType>
          <RequestNumber>3</RequestNumber>
          <FrequencyNumber>2</FrequencyNumber>
          <DelayNumber>1</DelayNumber>
      </User>
      <User UserID="User002">
          <FunctionName>FName22</FunctionName>
          <ActType>Update</ActType>
          <RequestNumber>5</RequestNumber>
          <FrequencyNumber>2</FrequencyNumber>
          <DelayNumber>3</DelayNumber>
      </User>      
  </Case>
</TestCase>


現要取得CaseID="Case001"且UserID="User002"的各元素的值該如何做?

------解决方案--------------------
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\Project\C#Test\WindowsApplication1\WindowsApplication1\XMLFile1.xml");

XmlNodeList nodeList = doc.SelectNodes("//TestCase//Case[@CaseID='Case001']//User[@UserID='User002']//*");
List<string> values = new List<string>();
foreach (XmlElement element in nodeList)
{

if (!values.Contains(element.InnerText))
{
Debug.WriteLine(string.Format("ElementName:{0} Value:{1}",element.Name ,element.InnerText));
values.Add(element.InnerText);
}
}
------解决方案--------------------
JScript code
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js">

</script>
</head>
<body>
<script type="text/javascript">
xmlDoc = loadXMLDoc("Xml1.xml");
var nodeList = xmlDoc.selectNodes("//Case[@CaseID='Case001']//User[@UserID='User002']//*"); 
for(i=0;i<nodeList.length;i++)
{
 alert(nodeList.item(i).nodeName+"-"+nodeList.item(i).childNodes[0].nodeValue);
}
</script>
</body>
</html>