请问怎么用c# 循环遍历我下面xml里面的节点的值?
答案:2 悬赏:0 手机版
解决时间 2021-11-11 18:51
- 提问者网友:浩歌待明月
- 2021-11-10 21:29
请问怎么用c# 循环遍历我下面xml里面的节点的值?
最佳答案
- 五星知识达人网友:几近狂妄
- 2021-11-10 22:11
下面是我贴的一段现成的代码..您需要改动一下..
另外如果您的子集不确定..就使用递归吧
注意:如果您的能力不是特别好.递归这个内容还是暂时忽略吧..
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();
}
}
}
}
}
}
另外如果您的子集不确定..就使用递归吧
注意:如果您的能力不是特别好.递归这个内容还是暂时忽略吧..
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();
}
}
}
}
}
}
全部回答
- 1楼网友:北城痞子
- 2021-11-10 22:32
用System.Xml.XmlTextReader,将xml的内容读取到DataSet里面
我的事VB代码,你自己转换下吧。
'
Function getXmlData(ByVal tableName As String) As DataSet
Dim ds As DataSet = Nothing
Dim xmlPath As String = "..\..\read\" & 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类
'
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("src\DB\write\subject.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
我的事VB代码,你自己转换下吧。
'
Function getXmlData(ByVal tableName As String) As DataSet
Dim ds As DataSet = Nothing
Dim xmlPath As String = "..\..\read\" & 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类
'
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("src\DB\write\subject.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
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯