如何遍历XML文件中的每个子节点? [英] How can I iterate though each child node in an XML file?
本文介绍了如何遍历XML文件中的每个子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个XML文件,我想遍历每个子节点收集信息的过程.
I have an XML File and i would like to iterate though each child node gathering information.
这是我的C#代码,它仅拾取一个节点,FieldData我想在其子节点上使用foreach.
Here is my C# code it only picks up one node, the FieldData i would like to use a foreach on its child nodes.
public void LoadXML() {
if (File.Exists("Data.xml")) {
//Reading XML
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Data.xml");
//Think something needs to reference Child nodes, so i may Foreach though them
XmlNodeList dataNodes = xmlDoc.SelectNodes("//FieldData");
TagContents[] ArrayNode;
foreach(XmlNode node in dataNodes) {
int Count = 0;
//int Max = node.ChildNodes.Count;
ArrayNode = new TagContents[Max];
ArrayNode[Count].TagName = node.Name;
ArrayNode[Count].TagValue = node.SelectSingleNode(ArrayNode[Count].TagName).InnerText;
Count = Count + 1;
}
} else {
MessageBox.Show("Could not find file Data.xml");
}
}
我的XML看起来像:
My XML Looks Something like:
<?xml version="1.0"?>
<FieldData>
<property_details_branch IncludeInPDFExport="Yes" Mod="20010101010101"/>
<property_details_inspection_date IncludeInPDFExport="Yes" Mod="20120726200230">20120727220230+0200</property_details_inspection_date>
<property_details_type_of_ownership IncludeInPDFExport="Yes" Mod="20120726134107">Freehold</property_details_type_of_ownership>
</FieldData>
推荐答案
您正在迭代FieldData节点,并且只有一个.要迭代其子节点,请输入:
You're iterating the FieldData nodes and you have only one. To iterate its child nodes write:
foreach (XmlNode node in dataNodes)
{
foreach (XmlNode childNode in node.ChildNodes)
{
这篇关于如何遍历XML文件中的每个子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文