我有以下 XML
<Company name="Kinanah Computers">
<Computer Name="Computer" type="Dell">
<Accessory type="screen" model="dell"/>
<Accessory type="mouse" model="dell"/>
<Accessory type="keyboard" model="dell"/>
</Computer>
<Computer Name="Computer" type="HP">
<Accessory type="screen" model="hp"/>
<Accessory type="mouse" model="chinese"/>
<Accessory type="keyboard" model="dell"/>
</Computer>
<Computer Name="Computer" type="HP">
<Accessory type="screen" model="hp"/>
<Accessory type="mouse" model="chinese"/>
<Accessory type="keyboard" model="dell"/>
</Computer>
<Computer Name="Computer" type="acer">
<Accessory type="screen" model="acer"/>
<Accessory type="mouse" model="acer"/>
<Accessory type="keyboard" model="acer"/>
</Computer>
</Company>
我想做的是跳过HP电脑,如果它的类型是HP,有谁能告诉我该怎么做吗?
我正在使用以下 C# 代码:
var stream = new StringReader(instanceXml/*the xml above*/);
var reader = XmlReader.Create(stream);
var hpCount = 0;
reader.MoveToContent();
while (reader.Read())
{
if(reader.NodeType == XmlNodeType.Element)
{
if(reader.GetAttribute("Name") == "Computer" && reader.GetAttribute("type") == "HP")
{
if(hpCount >1)
{
reader.Skip();
continue;
}
hpCount++;
}
}
}
但是 Skip 不起作用,读取的下一个元素是
<Accessory type="screen" model="hp"/>
关于如何跳过这些行的任何帮助?谢谢你。