2011-03-14 80 views
1

我有一个这样的XML文件模板来自一些IC芯片,我希望能够将每个Pins Test类型数据存储为一个数组,以便稍后我可以在笛卡尔图上绘制它。可以嵌套XPath迭代吗?

的数据结构是这样的: <NUMBER></NUMBER>是IC的特定引脚的名称 <Curve Count="">被的完成到管脚测试数 <Type></Type>是测试

的类型和存在一些电压值和电流值那我必须放入一个数组,以便以后使用它。

无论如何!我的问题是如何获得这些值?我不要求直接回答(但它是appriciated),但一些指导,让我找到线程真的appriciated。

编辑:如果somone亲切地给我一个代码,从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> 
+0

因为XPath 1.0节点集是唯一的无序节点集,所以没有办法表达一个像你想要分组的分层结构。 – 2011-03-14 16:14:08

回答

0

就像我喜欢使用LINQ和XDocument,这是XPath更简单的情况。

鉴于你的XML,这个XPath查找所有VIPair元素与给定NumberCurve元素与给定Type下一个Pin元素:

/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元素缺少这些子元素之一。 (或者,您可能实际上想要的代码在发生异常时抛出异常;这实际上取决于源数据的质量。)

+0

非常感谢,它工作得非常好!我只需要在你的coud中编辑.Value to .InnerText – 2011-03-14 21:27:53

0

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

该代码尚未经过测试

+0

谢谢,我试过这段代码并修正了错别字,但它返回null。你能再看一下吗?我无法修复您的代码以便正常工作。 – 2011-03-14 16:57:20

0

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