嵌套 XPath 迭代可能吗?

发布于 2024-10-21 14:01:44 字数 3617 浏览 1 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好听的两个字的网名 2024-10-28 14:01:44

尽管我很喜欢使用 LINQ 和 XDocument,但在这种情况下 XPath 更简单。

给定您的 XML,此 XPath 会查找具有给定 NumberPin 元素下和 Curve 下的所有 VIPair 元素具有给定 Type 的 code> 元素:

/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 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 a Pin element with a given Number and under a Curve element with a given Type:

/Document/Device/Pins/Pin[Number='A1']/Curves/Curve[Type='PreStress']/VIPairs/VIPair

Using an XmlDocument, the code to get your three voltage and current values would look like this:

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;
}

Using an XDocument, the code would be:

var values = doc.XPathSelectElements(xpath)
   .Select(x => new { Voltage = x.Element("Voltage").Value(),
                      Current = x.Element("Current").Value() });

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 a VIPair 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.)

明明#如月 2024-10-28 14:01:44

查看 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()
        });

代码尚未经过测试

Take a look at XDocument

For your particular case it would probably look something like

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()
        });

That code has not been tested

李白 2024-10-28 14:01:44

您可以使用一些 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
                            }
                    }
            };

You can use some LINQ-to-XML, like this:

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
                            }
                    }
            };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文