C# 嵌套XPath迭代是否可能?
我有一个类似这样的XML文件模板,它来自一些IC芯片,我希望能够将每个引脚测试类型的数据存储为一个数组,以便以后我可以在笛卡尔图上绘制它 数据结构如下所示:C# 嵌套XPath迭代是否可能?,c#,xml,xpath,nested,iteration,C#,Xml,Xpath,Nested,Iteration,我有一个类似这样的XML文件模板,它来自一些IC芯片,我希望能够将每个引脚测试类型的数据存储为一个数组,以便以后我可以在笛卡尔图上绘制它 数据结构如下所示: 是IC特定pin的名称 是对pin进行的测试数 是一种测试类型 还有一些电压值和电流值,我必须放在一个数组中,以便以后使用 无论如何!我的问题是如何获得这些值?我不要求一个直接的答案(但这是值得赞赏的),但一些指导,使我找到线程是真正值得赞赏的 编辑:如果有人好心给我一个代码,有3个电压和3个电流值从A1我可以很容易地得到的想法,并继续自己
是IC特定pin的名称
是对pin进行的测试数
是一种测试类型
还有一些电压值和电流值,我必须放在一个数组中,以便以后使用
无论如何!我的问题是如何获得这些值?我不要求一个直接的答案(但这是值得赞赏的),但一些指导,使我找到线程是真正值得赞赏的
编辑:如果有人好心给我一个代码,有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>
NEC555
A1
预应力
-2
-0.003
-1
-0.002
-0
-0.001
预发邮件
-2
-0.003
-1
-0.002
-0
-0.001
失败后
地下一层
预应力
-3
-0.005
-1
-0.002
-0
-0.001
预发邮件
-3
-0.003
-1
-0.002
-0
-0.001
失败后
看一看
对于你的特殊情况,它可能看起来像
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()
});
该代码尚未测试您可以使用一些LINQ到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
}
}
};
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;
}
尽管我非常喜欢使用LINQ和XDocument,但在这种情况下XPath更简单 给定XML,此XPath将在具有给定编号的
Pin
元素下以及具有给定类型的曲线的元素下查找所有VIPair
元素:
/Document/Device/Pins/Pin[Number='A1']/Curves/Curve[Type='PreStress']/VIPairs/VIPair
使用XmlDocument
,获取三个电压和电流值的代码如下所示:
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
}
}
};
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() });
除非您的文档之前已根据架构进行过验证,否则您可能希望在XPathVIPair[Current and Voltage]
中进行最后一次节点测试,以便在有VIPair
元素缺少其中一个子元素时,代码不会失败。(或者,如果发生这种情况,您可能真的希望代码抛出异常;这实际上取决于源数据的质量。)因为XPath 1.0节点集是一个唯一的无序节点集,所以无法表达您想要分组的层次结构。谢谢,我尝试了这段代码并修复了拼写错误,但它返回null。请你再看一下好吗?我还无法修复你的代码。非常感谢,它工作得非常好!我只是需要在你的coud.Value中编辑到.InnerText