- 苏州马小云
-
下面是我贴的一段现成的代码..您需要改动一下..
另外如果您的子集不确定..就使用递归吧
注意:如果您的能力不是特别好.递归这个内容还是暂时忽略吧..
GetXmlData()
{
ArrayList headersubList = new ArrayList();
//获取当前XML文档的根 一级
XmlNode oNode = m_oDom.DocumentElement;
//获取根节点的所有子节点列表
XmlNodeList oList = oNode.ChildNodes;
//标记当前节点
XmlNode oCurrentNode;
//遍历所有二级节点
for (int i = 0; i < oList.Count; i++)
{
//二级
oCurrentNode = oList[i];
//检测当前节点的名称,节点的值是否与已知匹配
if (oCurrentNode.Name.ToLower().Equals("store"))
{
//检测是否有子节点 三级
if (oCurrentNode.HasChildNodes)
{
Console.WriteLine(oCurrentNode.ChildNodes.Count);
//遍历当前节点的所有子节点
for (int n = 0; n < oCurrentNode.ChildNodes.Count; n++)
{
//检测当前节点的子节点名称是否与已知匹配
if (oCurrentNode.ChildNodes[n].Name.ToLower() == "sale")
{
//获取该节点所有属性,以供查询
XmlNamedNodeMap oMap = oCurrentNode.ChildNodes[n].Attributes;
Hashtable headItem = new Hashtable();
headItem.Add("Id", oCurrentNode.Attributes.GetNamedItem("Id").Value);
headItem.Add("Name", oCurrentNode.Attributes.GetNamedItem("Name").Value);
headItem.Add("OrderNo", oMap.GetNamedItem("OrderNo").Value);
headItem.Add("Qty", oMap.GetNamedItem("Qty").Value);
headersubList.Add(headItem);
Console.WriteLine(oCurrentNode.Attributes.GetNamedItem("Id").Value);
Console.WriteLine(oCurrentNode.Attributes.GetNamedItem("Name").Value);
Console.WriteLine(oMap.GetNamedItem("OrderNo").Value);
Console.WriteLine(oMap.GetNamedItem("Qty").Value);
Console.ReadLine();
}
}
}
}
}
}
- 左迁
-
用System.Xml.XmlTextReader,将xml的内容读取到DataSet里面
我的事VB代码,你自己转换下吧。
"/*************************************************************
"功能: 抽取DataSet
"输入: tableName 表名
"输出: DataSet 读取xml
"*************************************************************/
Function getXmlData(ByVal tableName As String) As DataSet
Dim ds As DataSet = Nothing
Dim xmlPath As String = ".... ead" & tableName & ".xml"
If File.Exists(xmlPath) Then
Dim fsReadXml As New System.IO.FileStream(xmlPath, System.IO.FileMode.Open)
" Create an XmlTextReader to read the file.
Dim xmlReader As New System.Xml.XmlTextReader(fsReadXml)
" Read the XML document into the DataSet.
ds.ReadXml(xmlReader)
" Close the XmlTextReader
xmlReader.Close()
End If
Return ds
End Function
然后操作dataset就行了。
或者用XmlDocument类
"/*************************************************************
"功能: 获取xml中的数据集
"输入: filter xml中node路径
"输入: xmlPath xml文件路径
"输出: xmlNodeList xml数据集
"*************************************************************/
Function getXmlList(ByVal xmlPath As String) As XmlNodeList
Dim xmlDoc As New XmlDocument
Dim xmlNodeList As XmlNodeList
Dim i As Integer = 0
Dim j As Integer = 0
Dim filter As String = "//NewDataSet/Table"
Try
If (Not Directory.Exists(xmlPath.Substring(0, xmlPath.LastIndexOf("")))) Then
Directory.CreateDirectory(xmlPath.Substring(0, xmlPath.LastIndexOf("")))
End If
xmlDoc.Load(xmlPath)
xmlNodeList = xmlDoc.SelectNodes(filter)
Catch ex As Exception
xmlDoc = Nothing
xmlNodeList = Nothing
End Try
Return xmlNodeList
End Function
调用方法:
Dim xmlNodeList As XmlNodeList
Dim nodeKid As String = ""
Dim nodeAreaDiv As String = ""
Dim count As Integer = 0
Dim areaCol As String = ""
Try
"获取xml文件
xmlNodeList = Common.getXmlList("srcDBwritesubject.xml")
If xmlNodeList.Count > 0 Then
For i = 0 To xmlNodeList.Count - 1
nodeKid = xmlNodeList(i).ChildNodes.Item(10).InnerText.ToString()
nodeAreaDiv = Common.decode(xmlNodeList(i).ChildNodes.Item(23).InnerText.ToString())
If nodeKid = kid And nodeAreaDiv <> "allArea" Then
If areaCol.IndexOf(nodeAreaDiv) = -1 Then
areaCol += nodeAreaDiv + "|"
count += 1
End If
End If
Next i
End If
Catch ex As Exception
count = 0
Finally
If count = 0 Then
areaCol = "allArea"
End If
End Try