如何读取这样的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;
int id=2;
string xpath = String.Format(@ "//node[@id= " "{0} " "] ",id);
XmlNode xn = xd.SelectSingleNode(xpath);
if (xn != null) {
foreach (XmlNode x in xn.ChildNodes) {
Console.WriteLine( "name:{0}\tvalue:{1} ", x.Name, x.InnerText);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
Console.WriteLine( "END ");
Console.Read();
}
}
}
=====运行结果
name:a value:3t
name:f value:gg
name:r value:fe
name:k value:ls
END
[解决办法]
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath( "your.xml "));
XmlNodeList nodeList=xmlDoc.SelectNodes(节点路径);
Labe1.text =nodeList[0].InnerText;
Labe2.text =nodeList[1].InnerText;
Labe3.text =nodeList[2].InnerText;
[解决办法]
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath( "your.xml "));
XmlNode xmlnode = doc.SelectSingleNode(//跟节点[@menuName= "1 ");
if (xmlnode != null)
{
Labe1.text=xmlnode.Attribut.[ "menuName "].Value.ToString();
Labe2.text=xmlnode.Attribut.[ "menuName "].Value.ToString();
Labe3.text=xmlnode.Attribut.[ "menuName "].Value.ToString();
}
[解决办法]
#region xml读写类Config
/// <summary>
/// Config 的摘要说明。
/// </summary>
public class Config
{
private String msFileName = null;
/// <summary>
/// 设置或获取文件名
/// </summary>
public String ConfigFile
{
get
{
return this.msFileName;
}
set
{
if(System.IO.File.Exists(value.Trim()))
{
this.msFileName = value.Trim();
}
}
}
private Stream msFile=null;
/// <summary>
/// 设置文件流
/// </summary>
///
public Stream setFileStream
{
set
{
this.msFile=value;
}
}
public Config()
{
this.msFileName = String.Empty;
}
public Config(String ConfigFile)
{
this.ConfigFile = ConfigFile.Trim();
}
private System.Xml.XmlDocument xmlDoc=new System.Xml.XmlDocument();
/// <summary>
/// 打开xml文档
/// </summary>
/// <param name= "ContentName "> </param>
/// <param name= "ContentValue "> </param>
/// <returns> </returns>
///
public void OpenXml()
{
try
{
if(!System.IO.File.Exists(this.msFileName))
{
xmlDoc.Load(this.msFile);
}
else
{
xmlDoc.Load(this.msFileName);
}
}
catch(XmlException ex)
{
System.Console.WriteLine(ex.Message);
}
}
public bool ReadConfig(String ContentName, out String ContentValue)
{
bool bFlag = false;
ContentValue = String.Empty;
try
{
System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
string xx=xmlNode.InnerText;
ContentValue=xx;
//ContentValue = xmlNode.InnerText;
bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}
return bFlag;
}
public bool ReadConfig(String ContentName, String PropertyName, out String PropertyValue)
{
bool bFlag = false;
PropertyValue = String.Empty;
try
{
XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
XmlAttributeCollection xmlAttr = xmlNode.Attributes;
for(int i=0; i <xmlAttr.Count; ++i)
{
if (xmlAttr.Item(i).Name == PropertyName)
{
PropertyValue = xmlAttr.Item(i).Value;
bFlag = true;
break;
}
}
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}
return bFlag;
}
public bool WriteConfig(String ContentName, String ContentValue)
{
bool bFlag = false;
if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
}
try
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(this.msFileName);
System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
xmlNode.InnerText = ContentValue;
xmlDoc.Save(this.msFileName);
bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}
return bFlag;
}
public bool WriteConfig(String ContentName, String PropertyName, String PropertyValue)
{
bool bFlag = false;
if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
}
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.msFileName);
XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
XmlAttributeCollection xmlAttr = xmlNode.Attributes;
for (int i = 0; i < xmlAttr.Count; ++i)
{
if (xmlAttr.Item(i).Name == PropertyName)
{
xmlAttr.Item(i).Value = PropertyValue;
bFlag = true;
break;
}
}
xmlDoc.Save(this.msFileName);
bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
}
return bFlag;
}
}
#endregion