Xml文件具有以下行:
<Action>
<Opcode
Id="20">"SPEED_L" 0 "#4900" 0
</Opcode>
<Opcode
Id="20">"Torque_L" 0 "#4900" 0
</Opcode>
</Action>
我要检查元素“ Action”是否具有元素“ Opcode”,其中包含ID = 20,标签名称=“ SPEED_L”?
谢谢!
答案 0 :(得分:0)
您可以使用XDocument
来加载xml,然后使用linq可以实现布尔结果,如
XDocument doc = XDocument.Load(@"Path to your xml file");
bool isPresent = doc.Descendants("Action")
.Elements("Opcode")
.Any(x => x.Attribute("Id").Value == "20" && x.Value.Contains("SPEED_L"));
if (isPresent == false)
{
//Add new one
}
输出:
答案 1 :(得分:0)
您可以这样做 data.xml
<Action>
<Opcode
Id="20">
"SPEED_L" 0 "#4900" 0
</Opcode>
<Opcode
Id="20">
"Torque_L" 0 "#4900" 0
</Opcode>
</Action>
C#代码:
XDocument xdoc = XDocument.Load("data.xml");
var test = from element in xdoc.Descendants("Opcode")
where element.Attribute("Id").Value == "20" && element.Value.Contains("SPEED_L") select element;
var result = test.FirstOrDefault();