【发布时间】:2021-02-23 07:36:57
【问题描述】:
这是 XML:
<MatML_Doc>
<Material>
<BulkDetails>
<Name>ABS</Name>
<Class> <Name>PLASTIC</Name></Class>
<Subclass> <Name>ABS Polymer</Name></Subclass>
<PropertyData property="Material_Type">
<Data format="string">IsotropicMaterial</Data>
</PropertyData>
<PropertyData property="Version">
<Data format="string">4.0</Data>
</PropertyData>
<PropertyData property="Category">
<Data format="string">PLASTIC</Data>
</PropertyData>
<PropertyData property="CoatingsStudioMaterialName">
<Data format="string">ABS Plastic</Data>
</PropertyData>
<PropertyData property="CoatingsVisualizationColor">
<Data format="exponential">168, 168, 168</Data>
</PropertyData>
<PropertyData property="ColorID">
<Data format="integer">87</Data>
</PropertyData>
</BulkDetails>
</Material>
<MatML_Doc>
如果节点“Name”的值为 ABS,我想获取数据属性“ColorID”的值(我想要数字 87)。 通常的程序是什么?
我在这里添加我的解决方案:
private static int GetColorIdInXmlByMaterial(string material, XmlDocument doc)
{
XmlElement element = doc.DocumentElement;
XmlNodeList xmlNodeList = element.SelectNodes("//BulkDetails");
foreach(XmlNode node in xmlNodeList)
{
if (node["Name"].InnerText.Equals(material))
{
return Convert.ToInt32(node["ColorID"].InnerText);
}
}
return 0;
}
【问题讨论】:
-
先获取名称,然后检查是否为“ABS”,即可获取颜色ID。