我遇到了一个我无法正确阅读的XML文件。我知道这是一个好文件;我只是在做一些愚蠢的事情。
以下是XML的示例:
- <Army xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ArmyName>Simple Army</ArmyName>
<Description>A file for test loading.</Description>
<LastId>2</LastId>
- <Units xmlns:a="http://schemas.datacontract.org/2004/07/ModelLib">
- <a:Unit>
<a:Accuracy i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
<a:Ammunition>30</a:Ammunition>
<a:CO>-1</a:CO>
<a:CloseCombat>1</a:CloseCombat>
<a:CommandLevel>1</a:CommandLevel>
<a:GetsFirstShotBonus>true</a:GetsFirstShotBonus>
<a:HQ>true</a:HQ>
<a:ID>0</a:ID>
<a:KStrength>1</a:KStrength>
<a:Leadership>50</a:Leadership>
<a:Morale>50</a:Morale>
<a:Name>General Smith</a:Name>
<a:Quality>50</a:Quality>
<a:Range>150</a:Range>
<a:Reload>30</a:Reload>
<a:Shooting>1</a:Shooting>
<a:SkirmishOption>Never</a:SkirmishOption>
<a:Strength>1000</a:Strength>
- <a:Subordinates>
- <a:Unit>
- <a:Accuracy xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
<b:double>100</b:double>
</a:Accuracy>
<a:Ammunition>30</a:Ammunition>
<a:CO>0</a:CO>
<a:CloseCombat>1</a:CloseCombat>
<a:CommandLevel>2</a:CommandLevel>
<a:GetsFirstShotBonus>true</a:GetsFirstShotBonus>
<a:HQ>true</a:HQ>
<a:ID>1</a:ID>
<a:KStrength>1</a:KStrength>
<a:Leadership>50</a:Leadership>
<a:Morale>50</a:Morale>
<a:Name>Major Disaster</a:Name>
<a:Quality>50</a:Quality>
<a:Range>150</a:Range>
<a:Reload>30</a:Reload>
<a:Shooting>1</a:Shooting>
<a:SkirmishOption>Never</a:SkirmishOption>
<a:Strength>1000</a:Strength>
- <a:Subordinates>
- <a:Unit>
- <a:Accuracy xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<b:double>1</b:double>
<b:double>1</b:double>
<b:double>0.996</b:double>
<b:double>0.995</b:double>
<b:double>0.993</b:double>
<b:double>0.991</b:double>
<b:double>0.989</b:double>
<b:double>0.986</b:double>
<b:double>0.983</b:double>
<b:double>0.979</b:double>
<b:double>0.977</b:double>
<b:double>0.974</b:double>
<b:double>0.972</b:double>
<b:double>0.969</b:double>
<b:double>0.965</b:double>
<b:double>0.962</b:double>
<b:double>0.96</b:double>
<b:double>0.956</b:double>
<b:double>0.95</b:double>
<b:double>0.945</b:double>
<b:double>0.938</b:double>
<b:double>0.931</b:double>
<b:double>0.923</b:double>
<b:double>0.912</b:double>
<b:double>0.9</b:double>
<b:double>0.885</b:double>
<b:double>0.869</b:double>
<b:double>0.846</b:double>
<b:double>0.827</b:double>
<b:double>0.804</b:double>
<b:double>0.781</b:double>
<b:double>0.76</b:double>
<b:double>0.737</b:double>
<b:double>0.713</b:double>
<b:double>0.688</b:double>
<b:double>0.66</b:double>
<b:double>0.631</b:double>
<b:double>0.603</b:double>
<b:double>0.576</b:double>
<b:double>0.547</b:double>
<b:double>0.52</b:double>
<b:double>0.494</b:double>
<b:double>0.467</b:double>
<b:double>0.441</b:double>
<b:double>0.42</b:double>
<b:double>0.4</b:double>
<b:double>0.383</b:double>
<b:double>0.364</b:double>
<b:double>0.343</b:double>
<b:double>0.322</b:double>
<b:double>0.301</b:double>
<b:double>0.281</b:double>
<b:double>0.265</b:double>
<b:double>0.25</b:double>
<b:double>0.233</b:double>
<b:double>0.216</b:double>
<b:double>0.202</b:double>
<b:double>0.19</b:double>
<b:double>0.177</b:double>
<b:double>0.167</b:double>
<b:double>0.155</b:double>
<b:double>0.148</b:double>
<b:double>0.139</b:double>
<b:double>0.132</b:double>
<b:double>0.126</b:double>
<b:double>0.118</b:double>
<b:double>0.113</b:double>
<b:double>0.107</b:double>
<b:double>0.103</b:double>
<b:double>0.1</b:double>
<b:double>0.095</b:double>
<b:double>0.09</b:double>
<b:double>0.085</b:double>
<b:double>0.083</b:double>
<b:double>0.079</b:double>
<b:double>0.076</b:double>
<b:double>0.073</b:double>
<b:double>0.07</b:double>
<b:double>0.067</b:double>
<b:double>0.065</b:double>
<b:double>0.064</b:double>
<b:double>0.062</b:double>
<b:double>0.058</b:double>
<b:double>0.056</b:double>
<b:double>0.052</b:double>
<b:double>0.05</b:double>
<b:double>0.048</b:double>
<b:double>0.046</b:double>
<b:double>0.043</b:double>
<b:double>0.04</b:double>
<b:double>0.037</b:double>
<b:double>0.034</b:double>
<b:double>0.031</b:double>
<b:double>0.029</b:double>
<b:double>0.027</b:double>
<b:double>0.022</b:double>
<b:double>0.021</b:double>
<b:double>0.016</b:double>
<b:double>0.011</b:double>
<b:double>0.003</b:double>
</a:Accuracy>
<a:Ammunition>30</a:Ammunition>
<a:CO>1</a:CO>
<a:CloseCombat>1</a:CloseCombat>
<a:CommandLevel>3</a:CommandLevel>
<a:GetsFirstShotBonus>true</a:GetsFirstShotBonus>
<a:HQ>false</a:HQ>
<a:ID>2</a:ID>
<a:KStrength>1</a:KStrength>
<a:Leadership>50</a:Leadership>
<a:Morale>50</a:Morale>
<a:Name>Captain Infantry</a:Name>
<a:Quality>50</a:Quality>
<a:Range>150</a:Range>
<a:Reload>30</a:Reload>
<a:Shooting>1</a:Shooting>
<a:SkirmishOption>Never</a:SkirmishOption>
<a:Strength>1000</a:Strength>
<a:Subordinates />
<a:Type>Infantry</a:Type>
</a:Unit>
</a:Subordinates>
<a:Type>HeadQuarters</a:Type>
</a:Unit>
</a:Subordinates>
<a:Type>HeadQuarters</a:Type>
</a:Unit>
</Units>
</Army>
我正在尝试使用此代码阅读它(根据以下建议更新):
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ScenarioFilename);
XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("d", "http://schemas.datacontract.org/2004/07/ModelLib");
var nodeList = xmlDoc.SelectNodes("/Army/Units/d:Unit", nsm);
bool tempval;
foreach (XmlNode node in nodeList)
{
tempHQ = node.SelectSingleNode("d:HQ", nsm).InnerText;
tempID = node.SelectSingleNode("d:ID", nsm).InnerText;
Boolean.TryParse(tempHQ, out tempval);
RedArmy[Int32.Parse(tempID)].IsHQ = tempval;
}// foreach
但是,nodelist返回0项。令我困惑的事情之一是何时在阅读中使用'd:'或'a:'。规则是什么?
Army
和Units
没有命名空间,但你选择它们就像它在"http://schemas.datacontract.org/2004/07/ModelLib"
命名空间(通过d
前缀)。
更改
var nodeList = xmlDoc.SelectNodes("/d:Army/d:Units/d:Unit", nsm);
至
var nodeList = xmlDoc.SelectNodes("/Army/Units/d:Unit", nsm);
然后,您继续将名称空间前缀切换到a
:
tempHQ = node.SelectSingleNode("a:HQ", nsm).InnerText;
tempID = node.SelectSingleNode("a:ID", nsm).InnerText;
那些也应该像最初定义的那样,d
:
nsm.AddNamespace("d", "http://schemas.datacontract.org/2004/07/ModelLib");
(或者像XML一样切换到a
。无论哪种方式,都要保持一致。)