请问怎么用c# 循环遍历我下面xml里面的节点的值?
<?xmlversion="1.0"encoding="utf-8"?><CustomerOrderDelivery><CustomerOperateFlag="Inse...
<?xml version="1.0" encoding="utf-8" ?>
<CustomerOrderDelivery>
<Customer OperateFlag="Insert">
<AppointmentId>1122</AppointmentId>
<CustomerNo>000123456</CustomerNo>
<LastName>山田</LastName>
<FirstName>将司</FirstName>
<LastNameRuby>yamada</LastNameRuby>
<FirstNameRuby>masashi</FirstNameRuby>
<bLastName>村上</bLastName>
<bFirstName>知子</bFirstName>
<bLastNameRuby>murakami</bLastNameRuby>
<bFirstNameRuby>tomoko</bFirstNameRuby>
<WeddingDate>2011-12-01</WeddingDate>
<area_cd>HBC</area_cd>
<ManagementId>110711T1600</ManagementId>
<appuser_name>testUser</appuser_name>
<app_time>2011-12-01</app_time>
</Customer>
<Orders>
<Order OperateFlag="Insert">
<SourceOrder>1001</SourceOrder>
<item_cd>LUX-NOIR45C</item_cd>
<ItemPropertys>
<isPs>True</isPs>
<Cover>blue</Cover>
<Film>GLOSSY</Film>
<CoverTitle>MyAlbum</CoverTitle>
<CoverDate>2013-01-01</CoverDate>
<CoverName>ワタベ</CoverName>
</ItemPropertys>
<Price1>2000</Price1>
<Remark></Remark>
"<Delivery>
<Quantity>1</Quantity>
<Receiver>モーランドスタジオ宛</Receiver>
<ZipCode></ZipCode>
<Address></Address>
<Tel></Tel>
</Delivery>
</Order>
</Orders>
</CustomerOrderDelivery> 展开
<CustomerOrderDelivery>
<Customer OperateFlag="Insert">
<AppointmentId>1122</AppointmentId>
<CustomerNo>000123456</CustomerNo>
<LastName>山田</LastName>
<FirstName>将司</FirstName>
<LastNameRuby>yamada</LastNameRuby>
<FirstNameRuby>masashi</FirstNameRuby>
<bLastName>村上</bLastName>
<bFirstName>知子</bFirstName>
<bLastNameRuby>murakami</bLastNameRuby>
<bFirstNameRuby>tomoko</bFirstNameRuby>
<WeddingDate>2011-12-01</WeddingDate>
<area_cd>HBC</area_cd>
<ManagementId>110711T1600</ManagementId>
<appuser_name>testUser</appuser_name>
<app_time>2011-12-01</app_time>
</Customer>
<Orders>
<Order OperateFlag="Insert">
<SourceOrder>1001</SourceOrder>
<item_cd>LUX-NOIR45C</item_cd>
<ItemPropertys>
<isPs>True</isPs>
<Cover>blue</Cover>
<Film>GLOSSY</Film>
<CoverTitle>MyAlbum</CoverTitle>
<CoverDate>2013-01-01</CoverDate>
<CoverName>ワタベ</CoverName>
</ItemPropertys>
<Price1>2000</Price1>
<Remark></Remark>
"<Delivery>
<Quantity>1</Quantity>
<Receiver>モーランドスタジオ宛</Receiver>
<ZipCode></ZipCode>
<Address></Address>
<Tel></Tel>
</Delivery>
</Order>
</Orders>
</CustomerOrderDelivery> 展开
2个回答
展开全部
下面是我贴的一段现成的代码..您需要改动一下..
另外如果您的子集不确定..就使用递归吧
注意:如果您的能力不是特别好.递归这个内容还是暂时忽略吧..
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();
}
}
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用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 = "..\..\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类
'/*************************************************************
'功能: 获取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("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代码,你自己转换下吧。
'/*************************************************************
'功能: 抽取DataSet
'输入: tableName 表名
'输出: DataSet 读取xml
'*************************************************************/
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类
'/*************************************************************
'功能: 获取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("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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询