日期:2014-05-19  浏览次数:20779 次

如何读取这样的XML?
<1>
<a> 3 </a>
<f> g </f>
<r> f </r>
<k> l </k>
</1>
<2>
<a> 3t </a>
<f> gg </f>
<r> fe </r>
<k> ls </k>
</2>
<3>
<a> 3v </a>
<f> ge </f>
<r> fdd </r>
<k> lhht </k>
</3>
<4>
<a> 123 </a>
<f> dd </f>
<r> ff </r>
<k> lb </k>
</4>

我想当我查询进来的ID=?的时候进行查询!
之后读取这个接点下的所有的东西!
怎么办?在线等急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

------解决方案--------------------
private void previous_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
string strPath = "root/itemData/cell "; //查询路径
XmlNodeList nodeList = null;
string strItem = " "; //item值
string strKey = " "; //key值
string strValue = " "; //value值
try
{
xmlDoc.Load( "e:\\新建 文本文档 (2).xml ");

//获取所有的itemData项
nodeList = xmlDoc.SelectNodes(strPath);
//遍历所有节点
foreach (XmlNode node in nodeList)
{
strItem = node.Attributes[ "item "].InnerText;
//遍历所有字节点
foreach (XmlNode subNode in node.ChildNodes)
{
strKey=subNode.Attributes[ "key "].InnerText;
strValue = subNode.Attributes[ "value "].InnerText;

//strItem,strKey,strValue值都获取了,剩下就是对数据库操作,我就不再多说
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}



------解决方案--------------------
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Text;
using Microsoft.CSharp;
using System.Collections.Generic;
using System.Xml;

public class MyClass
{

public static void Main()
{
try
{
string xml = @ "
<root>
<node id= " "1 " ">
<a> 3 </a>
<f> g </f>
<r> f </r>
<k> l </k>
</node>
<node id= " "2 " ">
<a> 3t </a>
<f> gg </f>
<r> fe </r>
<k> ls </k>
</node>
<node id= " "3 " ">

<a> 3v </a>
<f> ge </f>
<r> fdd </r>
<k> lhht </k>
</node>
<node id= " "4 " ">

<a> 123 </a>
<f> dd </f>
<r> ff </r>
<k> lb </k>
</node>
</root>
";

XmlDocument xd = new XmlDocument();
xd.LoadXml(xml);
XmlElement xe = xd.DocumentElement;