<?xml version="1.0" encoding="utf-8" ?>
<game>
<gameinfo value = "eq">
<datainfo>
<table>report</table>
<baktable>report</baktable>
<type>0</type>
<database>eq</database>
<username>eq</username>
<password>123</password>
<totb>0.95</totb>
<set id="1"/>
<set id="2"/>
<set id="3"/> </datainfo> </gameinfo>
<gameinfo value = "eq1">
<datainfo>
<table>report</table>
<baktable>report</baktable>
<type>1</type>
<username>1</username>
<password>1</password>
<totb>1</totb>
<set id="1"/>
<set id="2"/>
<set id="3"/>
</datainfo>
</gameinfo>
</game>这个如何取datainfo中的值和下面id中的值 不同id的值需要放在不同的arrlist中 给个具体代码谢谢了
<game>
<gameinfo value = "eq">
<datainfo>
<table>report</table>
<baktable>report</baktable>
<type>0</type>
<database>eq</database>
<username>eq</username>
<password>123</password>
<totb>0.95</totb>
<set id="1"/>
<set id="2"/>
<set id="3"/> </datainfo> </gameinfo>
<gameinfo value = "eq1">
<datainfo>
<table>report</table>
<baktable>report</baktable>
<type>1</type>
<username>1</username>
<password>1</password>
<totb>1</totb>
<set id="1"/>
<set id="2"/>
<set id="3"/>
</datainfo>
</gameinfo>
</game>这个如何取datainfo中的值和下面id中的值 不同id的值需要放在不同的arrlist中 给个具体代码谢谢了
然后给DATASET 就行
你要是读节点,可以用xpath
这样比较灵活
想弄成什么格式的都行比如
XmlDocument doc = new XmlDocument();//构造doc
XmlNodeList list = doc.SelectNodes("game/gameinfo");
这个list里面就是两个gameinfo 的节点集合
接下来你想怎么分就怎么分啊
昨天刚做了一个遍历xml然后映射到树性空间的demo
//打开一个xml文件
public bool OpenXmlDoc()
{
try
{
if(!m_bOpenStat)
{
m_xmlDoc=new XmlDocument();
m_xmlDoc.Load((m_strXmlPath.Equals(string.Empty))?"config\\Appcfg.xml":m_strXmlPath);
}
m_bOpenStat=true;
}
catch(Exception ex)
{
return false;
} return true;
}
//读取元素内容和属性 public string GetElementAttr(string eleName)
{
if(!m_bOpenStat) OpenXmlDoc(); string strValue=string.Empty; XmlNodeList lst=m_xmlDoc.GetElementsByTagName(eleName); strValue=(lst.Item(0).InnerText==null)?string.Empty:lst.Item(0).InnerText;
System.Xml.XmlAttributeCollection cols=lst.Item(0).Attributes;
foreach(System.Xml.XmlAttribute attr in cols)
{ } return strValue; }//解析xmlDoc
public void ListDoc(System.Xml.XmlDocument xmlDoc)
{
System.Xml.XmlNodeList nodeLst=xmlDoc.ChildNodes;
foreach(System.Xml.XmlNode node in nodeLst)
{
if(node.NodeType==XmlNodeType.Element)
ListNodes(null,node);
}
} public void ListNodes(System.Windows.Forms.TreeNode parentTreeViewNode,System.Xml.XmlNode node)
{
if(node.NodeType!=XmlNodeType.Element) return;
TreeNode tnode=new TreeNode(node.Name);
if(parentTreeViewNode!=null)
parentTreeViewNode.Nodes.Add(tnode);
else
this.tvw_Xml.Nodes.Add(tnode);
foreach(System.Xml.XmlNode childNode in node.ChildNodes)
{
ListNodes(tnode,childNode);
}
}----------
看看这几个类吧
XmlDocument
XmlNode
XmlAttribute
主要步骤
1 打开xml文件 m_xmlDoc=new XmlDocument();
m_xmlDoc.Load("menu.xml");
2 获得一个元数
XmlNodeList lst=m_xmlDoc.GetElementsByTagName(eleName); //eleName比如数是gameinfo
3 在第二步lst中,遍历,
System.Xml.XmlAttributeCollection cols=lst.Item(0).Attributes;//可以获得属性集
根据value属性<gameinfo value = "eq"> 匹配的元素
4 获得子元数集node.ChildNodes
如此向下
ds.readxml("aa.xml")
然后操作这个ds不用教了吧
是否可以将 report 这个 InnerText取出来
<type>0</type><database>eq</database><username>eq</username><password>123</password><totb>0.95</totb>值取出来存到变量中去 然后把所有set id 的值放进一个arraylist中
XmlDocument doc = new XmlDocument();
doc.Load(path);
string table = doc.SelectSingleNode("game/gameinfo[@value='eq']/datainfo/table").InnerText;
string baktable = doc.SelectSingleNode("game/gameinfo[@value='eq']/datainfo/baktable").InnerText;
.........
XmlNodeList list = doc.SelectNodes("game/gameinfo[@value='eq']/datainfo/set");
ArrayList set = new ArrayList();
foreach(XmlNode temp in list)
{
set.Add(temp.Attributes["id"].Value)
}
上面的字符串我只是举了两个例子
后面的set这个arraylist就是你想要得所有set节点的属性的值
可以方便的根据你的需要
得到你想要得值