【发布时间】:2016-06-23 19:21:48
【问题描述】:
这是给定的 XML:
<NewDataSet>
<Table>
<ProductCode>0120314</ProductCode>
<SpecificationItemName>USB</SpecificationItemName>
<SpecificationItemValues><Values xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Value>USB 2.0</Value></Values></SpecificationItemValues>
</Table>
<Table>
<ProductCode>0987046</ProductCode>
<SpecificationItemName>Other</SpecificationItemName>
<SpecificationItemValues><Values xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Value>1 x PCIe 2.0 x16</Value><Value>2 x PCIe 2.0 x1</Value></Values></SpecificationItemValues>
</Table>
</NewDataSet>
这是我读取 ProductCode、SpectificationItemName 和 SpecificationItemValues 的完整值的解决方案。 你能帮我按值(一个一个,如果不止一个)阅读 SpecificationItemValues 吗?谢谢。
这是我的代码:
foreach (XmlNode nodeSpecification in xmlDokument.SelectSingleNode("//NewDataSet"))
{
if (nodeSpecification.Name == "Table")
{
foreach (XmlNode nodeElements in nodeSpecification)
{
if (nodeElements.Name == "ProductCode")
{
MessageBox.Show(nodeElements.InnerText);
}
if (nodeElements.Name == "SpecificationItemName")
{
MessageBox.Show(nodeElements.InnerText);
}
if (nodeElements.Name == "SpecificationItemValues")
{
MessageBox.Show(nodeElements.InnerText);
}
} //you were missing a closing } by the way
}
}
【问题讨论】:
-
您必须对“SpecificationItemValues”的内部文本值进行 Xml 解码,并使用 xml 文档的其他实例解析它们。这个答案应该给你一个想法stackoverflow.com/questions/6757019/…