我有一个xml文档,如下所示:
<customerList>
<customerRecord>
<customerName>David</customerName>
<customerAge>25</customerAge>
<customerAddress>1, Walking Street, 3000 Las Vegas</customerAddress>
<customerPIC>
<picList>
<picName>Rose</picName>
<picPosition>supervisor</picPosition>
<idType>1</idType>
<idNumber>90001000</idNumber>
</picList>
<picList>
<picName>James</picName>
<picPosition>supervisor</picPosition>
<idType>2</idType>
<idNumber>90002000</idNumber>
</picList>
</customerPIC>
<customerCompany>Facebook</customerCompany>
</customerRecord>
<customerRecord>
<customerName>Carrie</customerName>
<customerAge>25</customerAge>
<customerAddress>2, Dancing Road, 2000 New Mexico</customerAddress>
<customerPIC>
<picList>
<picName>Maisie</picName>
<picPosition>secretary</picPosition>
<idType>1</idType>
<idNumber>90003000</idNumber>
</picList>
<picList>
<picName>John</picName>
<picPosition>HR</picPosition>
<idType>2</idType>
<idNumber>90004000</idNumber>
</picList>
</customerPIC>
<customerCompany>Uber</customerCompany>
</customerRecord>
<customerRecord>
<customerName>Dexter</customerName>
<customerAge>25</customerAge>
<customerAddress>3, Hugging Street, 3000 New Orleans</customerAddress>
<customerPIC>
<picList>
<picName>Jill</picName>
<picPosition>PA</picPosition>
<idType>1</idType>
<idNumber>90005000</idNumber>
</picList>
<picList>
<picName>Alex</picName>
<picPosition>supervisor</picPosition>
<idType>2</idType>
<idNumber>90006000</idNumber>
</picList>
</customerPIC>
<customerCompany>Google</customerCompany>
</customerRecord>
简而言之,我想比较每个customerRecord中每个picList的每个idNumber,再次将picString =“ 90001000”。由于在每个客户记录中将有两个picList,因此如果这些idNumber中的任何一个都不等于picString。我需要删除该customerRecord。
目前,我正在使用如下代码。由于picString可以位于两个picList中的任何一个中,因此我一直在思考如何进行比较,而不必冒险删除每个客户记录。
string picString = "90001000";
foreach (XmlNode customerRecord in customerList)
{
XmlNode customerPIC = customerRecord.SelectSingleNode("//pic:customerPIC", nsManager);
foreach (XmlNode picList in customerPIC)
{
XmlNode idNumber = picList.SelectSingleNode("//pic:idNumber", nsManager);
string idNumberString = idNumberString.InnerText;
if (idNumberString != picString)
customerRecord.RemoveAll();
}
}
谢谢
答案 0 :(得分:1)
我将使用一个标志来确定是否找到了一个等于“ 90001000”的picString
。
string picString = "90001000";
foreach (XmlNode customerRecord in customerList)
{
XmlNode customerPIC = customerRecord.SelectSingleNode("//pic:customerPIC", nsManager);
//let's consider the picstring wasn't found by default, for each customerRecord.
bool isPicStringFound = false;
foreach (XmlNode picList in customerPIC)
{
XmlNode idNumber = picList.SelectSingleNode("//pic:idNumber", nsManager);
string idNumberString = idNumberString.InnerText;
if (idNumberString == picString)
isPicStringFound = true; //we found it ! no need to delete
}
//no picString found ? delete !
if (!isPicStringFound)
customerRecord.RemoveAll();
//at next loop, the flag will be reset
}