嵌套 XPath 迭代可能吗?
我有一个像这样的 XML 文件模板,它来自一些 IC 芯片,我希望能够将每个引脚测试类型数据存储为数组,以便稍后我可以将其绘制在笛卡尔图上。
数据结构是这样的:
是 IC 特定引脚的名称
是对引脚进行的测试次数
是测试类型
,我必须将一些电压值和电流值放入数组中,以便稍后使用。
反正!我的问题是如何获得这些值?我不要求直接答案(但它是值得赞赏的),但一些让我找到线索的指南确实很受欢迎。
编辑:如果有人给我一个代码,让我从 A1 获得 3 个电压和 3 个电流值,我可以很容易地得到这个想法并继续自己。
这个 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>
I have a XML file template like this that comes from some IC chips, I want to be able to store each Pins Test type datas as an array so later I can draw it on a cartesian graph.
The data structure is like this:<NUMBER></NUMBER>
is name of the IC's particular pin<Curve Count="">
is number of tests done to a pin<Type></Type>
is type of test
and there are some voltage values and current values that I have to put in an array so I can use it later.
Anyway! My question is how can I get these values? I do not ask for a direct answer (but it is appriciated) but some guide that make me to find the thread is really appriciated.
EDIT: If somone kindly give me a code to have 3 voltage and 3 current values from A1 I can easily get the idea and continue myself.
This XML looks likes this:
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管我很喜欢使用 LINQ 和
XDocument
,但在这种情况下 XPath 更简单。给定您的 XML,此 XPath 会查找具有给定
Number
的Pin
元素下和Curve
下的所有VIPair
元素具有给定Type
的 code> 元素:使用
XmlDocument
,获取三个电压和电流值的代码将如下所示:使用 XDocument,代码将是:
除非您的文档之前已针对架构进行过验证,否则您可能需要在 XPath
VIPair[Current andVoltage]
中进行最后一个节点测试,以便在存在时代码不会失败VIPair
元素缺少这些子元素之一。 (或者,您可能实际上希望代码在发生这种情况时抛出异常;这实际上取决于源数据的质量。)Much as I like using LINQ and
XDocument
, this is a case where XPath is simpler.Given your XML, this XPath finds all of the
VIPair
elements under aPin
element with a givenNumber
and under aCurve
element with a givenType
:Using an
XmlDocument
, the code to get your three voltage and current values would look like this:Using an XDocument, the code would be:
Unless your document's been previously validated against a schema, you may want to make the last node test in the XPath
VIPair[Current and Voltage]
, so that the code doesn't fail if there's aVIPair
element that's missing one of those child elements. (Alternatively, you might actually want the code to throw an exception if that happens; it really depends on the quality of the source data.)查看 XDocument
对于您的具体情况它可能看起来像
代码尚未经过测试
Take a look at XDocument
For your particular case it would probably look something like
That code has not been tested
您可以使用一些 LINQ-to-XML,如下所示:
You can use some LINQ-to-XML, like this: