我有一个这样的XML文件模板,它来自一些IC芯片,我希望能够将每个管脚测试类型数据存储为一个数组,这样以后我就可以在笛卡尔图上绘制它。
数据结构如下:<NUMBER></NUMBER>是集成电路特定管脚的名称<Curve Count="">是对管脚进行的测试次数<Type></Type>是测试类型
还有一些电压值和电流值,我必须放在一个数组中,这样我以后才能使用它。
不管怎么说!我的问题是如何获得这些值?我不要求一个直接的答案(但它是接近的),但一些指导,使我找到线索是真的接近。
编辑:如果有人好心给我一个代码,有3个电压和3个电流值从A1,我可以很容易得到的想法,并继续我自己。
这个XML看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<Document>
<Device>NEC555</Device>
<Pins Count="3">
<Pin>
<Number>A1</Number>
<Curves Count ="3">
<Curve>
<Type>PreStress</Type>
<VIPairs Count="3">
<VIPair>
<Voltage>-2</Voltage>
<Current>-0.003</Current>
</VIPair>
<VIPair>
<Voltage>-1</Voltage>
<Current>-0.002</Current>
</VIPair>
<VIPair>
<Voltage>-0</Voltage>
<Current>-0.001</Current>
</VIPair>
</VIPairs>
</Curve>
<Curve>
<Type>PreFail</Type>
<VIPairs Count="3">
<VIPair>
<Voltage>-2</Voltage>
<Current>-0.003</Current>
</VIPair>
<VIPair>
<Voltage>-1</Voltage>
<Current>-0.002</Current>
</VIPair>
<VIPair>
<Voltage>-0</Voltage>
<Current>-0.001</Current>
</VIPair>
</VIPairs>
</Curve>
<Curve>
<Type>PostFail</Type>
<VIPairs Count="0">
</VIPairs>
</Curve>
</Curves>
</Pin>
<Pin>
<Number>B1</Number>
<Curves Count ="3">
<Curve>
<Type>PreStress</Type>
<VIPairs Count="3">
<VIPair>
<Voltage>-3</Voltage>
<Current>-0.005</Current>
</VIPair>
<VIPair>
<Voltage>-1</Voltage>
<Current>-0.002</Current>
</VIPair>
<VIPair>
<Voltage>-0</Voltage>
<Current>-0.001</Current>
</VIPair>
</VIPairs>
</Curve>
<Curve>
<Type>PreFail</Type>
<VIPairs Count="3">
<VIPair>
<Voltage>-3</Voltage>
<Current>-0.003</Current>
</VIPair>
<VIPair>
<Voltage>-1</Voltage>
<Current>-0.002</Current>
</VIPair>
<VIPair>
<Voltage>-0</Voltage>
<Current>-0.001</Current>
</VIPair>
</VIPairs>
</Curve>
<Curve>
<Type>PostFail</Type>
<VIPairs Count="0">
</VIPairs>
</Curve>
</Curves>
</Pin>
</Pins>
</Document>发布于 2011-03-15 03:52:59
虽然我很喜欢使用LINQ和XDocument,但这是一个XPath更简单的例子。
给定XML,此XPath将查找具有给定Number的Pin元素和具有给定Type的Curve元素下的所有VIPair元素
/Document/Device/Pins/Pin[Number='A1']/Curves/Curve[Type='PreStress']/VIPairs/VIPair使用XmlDocument,获取三个电压值和电流值的代码如下所示:
string number = "A1";
string type = "PreStress"
string xpath = string.Format(
"/Document/Device/Pins/Pin[Number='{0}']/Curves/Curve[Type='{1}']/VIPairs/VIPair",
number,
type);
foreach (XmlElement viPair in doc.SelectNodes(xpath))
{
string current = viPair.SelectSingleNode("Current").Value;
string voltage = viPair.SelectSingleNode("Voltage").Value;
}使用XDocument时,代码将为:
var values = doc.XPathSelectElements(xpath)
.Select(x => new { Voltage = x.Element("Voltage").Value(),
Current = x.Element("Current").Value() });除非您的文档以前已经根据某个模式进行了验证,否则您可能希望在XPath VIPair[Current and Voltage]中进行最后一个节点测试,这样即使有一个VIPair元素缺少其中一个子元素,代码也不会失败。(或者,如果发生这种情况,您可能真的希望代码抛出异常;这实际上取决于源数据的质量。)
发布于 2011-03-14 23:00:05
看一看XDocument
对于您的特定情况,它可能如下所示
XDocument doc = XDocument.Load("yourFilePath");
var data = (from pins in doc.Element("Pins").Descendants
from pin in pins.Element("Pin")
from curve in pin.Element("Curves").Element("Curve")
from pair in curve.Element("VIPairs").Descendants.Descendants()
select new {
Voltage = pair.Element("Voltage").Value(),
Current = pair.Element("Current").Value()
});该代码尚未经过测试
发布于 2011-03-15 02:35:31
您可以使用一些LINQ-to-XML,如下所示:
var doc = XDocument.Parse(xml);
var data = from pin in doc.Descendants("Pin")
select new
{
Number = pin.Element("Number").Value,
Curves =
from curve in pin.Element("Curves").Descendants("Curve")
select new
{
Type = curve.Element("Type").Value,
VIPairs =
from pair in curve.Descendants("VIPair")
select new
{
Voltage = pair.Element("Voltage").Value,
Current = pair.Element("Current").Value
}
}
};https://stackoverflow.com/questions/5300215
复制相似问题