2016-02-17 118 views
0
<physicalResource> 
    <prName>PRS_EID</prName> 
    <prDescription>PRS_EID</prDescription> 
    <physicalResourceCharacteristic> 
     <characteristic> 
      <name>eidno</name> 
      <value>SH001499000</value> 
     </characteristic> 
     <characteristic> 
      <name>flatno</name> 
      <value>14303</value> 
     </characteristic> 
    </physicalResourceCharacteristic> 
    </physicalResource> 

    <physicalResource> 
    <prName>PRS_OLT</prName> 
    <prDescription>PRS_OLT</prDescription>   
    <physicalResourceCharacteristic> 
     <characteristic> 
      <name>device</name> 
      <value>WC-OMU-AO01</value> 
     </characteristic> 
     <characteristic> 
      <name>frame</name> 
      <value>1</value> 
     </characteristic> 
     <characteristic> 
      <name>port</name> 
      <value>5</value>     
     </characteristic> 
    </physicalResourceCharacteristic> 
    </physicalResource> 

Hello Dears ..我有一個xml文件。它包含具有相同節點名稱的不同節點。在physicalResource節點下的示例中,我想提取prName和所有特徵的名稱和值。但我不能單獨解析它們。 我使用從多個節點中的單個節點解析出具有相同名稱的多個節點

nodeListPrs = root.SelectNodes("/physicalResource/physicalResourceCharacteristic/characteristic", nsmgr); 

它提取兩個節點的所有charactics值。我怎樣才能從單個物理資源節點提取這些參數?

回答

0

謝謝大家。我通過使用SelectSingleNode和SelectNodes級聯解決了:

XmlNodeList nodeListPrs = root.SelectNodes("/ns2:serviceOrder/resourceFacingService/physicalResource", nsmgr); 
      foreach (XmlNode book in nodeListPrs) 
      { 
       string prsName = book["prName"].InnerText; 

       try 
       { 
        nodeListPrsCh = book.SelectSingleNode("physicalResourceCharacteristic").SelectNodes("characteristic"); 

        foreach (XmlNode characteristics in nodeListPrsCh) 
        { 
         dataGridView3.Rows.Add(); 
         dataGridView3.Rows[i].Cells[0].Value = prsName; 

         try { string name = characteristics["name"].InnerText; dataGridView3.Rows[i].Cells[1].Value = name; } 
         catch { dataGridView3.Rows[i].Cells[1].Value = "-"; } 

         try { string value = characteristics["value"].InnerText; dataGridView3.Rows[i].Cells[2].Value = value; } 
         catch { dataGridView3.Rows[i].Cells[2].Value = "-"; } 

         i = i + 1; 
        } 
       } 

       catch 
       { 

        dataGridView3.Rows.Add(); 
        dataGridView3.Rows[i].Cells[0].Value = prsName; 
        dataGridView3.Rows[i].Cells[1].Value = "-"; 
        dataGridView3.Rows[i].Cells[2].Value = "-"; 
        i = i + 1; 
       } 
      } 
1

您可以使用xmldocument並將該xml加載到xmldocument,然後您可以使用selectsinglenode。這將有助於!

XmlDocument xdoc = new XmlDocument(); 
xdoc.LoadXml(xml); 
xdoc.DocumentElement.SelectSingleNode(path for the node..); 
0

使用XML的LINQ

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication78 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 

      var results = doc.Descendants("physicalResource").Select(x => new { 
       prName = (string)x.Element("prName"), 
       characteristics = x.Descendants("characteristic").Select(y => new { 
         name = (string)y.Element("name"), 
         value = (string)y.Element("value") 
        }).ToList() 
       }).ToList(); 

     } 
    } 
} 
+0

感謝您的回答。但我可以提取prName的。我的問題是提取特徵。我用selectnodes(physicalResource/physicalResourceCharacteristic/characteristic)。但輸出包含所有的物理資源。我想讓他們分別爲physicalResource節點1和physicalResource節點2 –

+0

更新我的代碼 – jdweng

0

要選擇與第一physicalResourceCharacteristic節點的相應characteristic節點的所有prName節點,你可以使用在SelectNodes的follwing XPath表達式。

​​

結果是

<?xml version="1.0" encoding="UTF-8"?> 
<prName>PRS_EID</prName> 
<characteristic> 
    <name>eidno</name> 
    <value>SH001499000</value> 
</characteristic> 
<characteristic> 
    <name>flatno</name> 
    <value>14303</value> 
</characteristic> 

我不知道如果這是你追求什麼。您可以迭代計數physicalResource構造[xxx] XPath表達式以獲取具有這些條目中的幾個條目的節點集。或者您可以省略[1]以獲得所有physicalResource的節點集,其前綴爲prName,但是具有混合節點類型。

相關問題