我有一個XML文件,我想遍歷每個子節點收集信息。如何迭代XML文件中的每個子節點?
這是我的C#代碼,它只拾取一個節點,FieldData我想在其子節點上使用foreach。
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看起來像:
<?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>
我只是想指出,在'foreach'循環設置'Count'爲零,做的東西,增量它並且設置回零。 – 2017-09-16 08:25:09