2013-03-15 506 views
1

我有下面的XML元素獲取過濾:通過屬性

<?xml version="1.0" encoding="utf-8"?> 
<assertions> 
    <assertion id="SLM_CekNonNegative"> 
     <satisfied count="36"/> 
     <unsatisfied count="4"> 
      <message xml:lang="id">(Record ID = ID_2) SLM: Jumlah bulan lalu, jumlah bulan laporan, jumlah hari tunggakan, dan jumlah tunggakan pokok tidak boleh kosong dan harus diisi >= 0</message> 
      <message xml:lang="id">(Record ID = ID_2) SLM: Jumlah bulan lalu, jumlah bulan laporan, jumlah hari tunggakan, dan jumlah tunggakan pokok tidak boleh kosong dan harus diisi >= 0</message> 
      <message xml:lang="id">(Record ID = ID_2) SLM: Jumlah bulan lalu, jumlah bulan laporan, jumlah hari tunggakan, dan jumlah tunggakan pokok tidak boleh kosong dan harus diisi >= 0</message> 
      <message xml:lang="id">(Record ID = ID_2) SLM: Jumlah bulan lalu, jumlah bulan laporan, jumlah hari tunggakan, dan jumlah tunggakan pokok tidak boleh kosong dan harus diisi >= 0</message> 
     </unsatisfied> 
    </assertion> 
    <assertion id="Base_CekJangkaWaktuMulaiLebihMudaJatuhTempo"> 
     <satisfied count="10"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="SLM_CekJumlahBulanKrgDrNilaiKontrak"> 
     <satisfied count="10"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_CekTanggalLebihMudaTanggalPelaporan"> 
     <satisfied count="10"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="SLM_CekNilaiKontrak"> 
     <satisfied count="2"/> 
     <unsatisfied count="8"> 
      <message xml:lang="id">(Record ID = ID_10) Nilai kontrak harus > 0</message> 
      <message xml:lang="id">(Record ID = ID_9) Nilai kontrak harus > 0</message> 
      <message xml:lang="id">(Record ID = ID_8) Nilai kontrak harus > 0</message> 
      <message xml:lang="id">(Record ID = ID_7) Nilai kontrak harus > 0</message> 
      <message xml:lang="id">(Record ID = ID_6) Nilai kontrak harus > 0</message> 
      <message xml:lang="id">(Record ID = ID_5) Nilai kontrak harus > 0</message> 
      <message xml:lang="id">(Record ID = ID_2) Nilai kontrak harus > 0</message> 
      <message xml:lang="id">(Record ID = ID_1) Nilai kontrak harus > 0</message> 
     </unsatisfied> 
    </assertion> 
    <assertion id="SLM_CekGolNasabah"> 
     <satisfied count="9"/> 
     <unsatisfied count="1"> 
      <message xml:lang="id">(Record ID = ID_2) Golongan nasabah tidak boleh diisi sandi bank pelapor sendiri</message> 
     </unsatisfied> 
    </assertion> 
    <assertion id="BS-IF_CekSLMVls"> 
     <satisfied count="1"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="BS-IF_CekSLMIDR"> 
     <satisfied count="1"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_CekHubunganTidakTerkaitStatusLainnya"> 
     <satisfied count="8"/> 
     <unsatisfied count="2"> 
      <message xml:lang="id">(Record ID = ID_3) Base: "Status hubungan dengan Bank" harus terisi sandi 9 jika Kolom "Klasifikasi Nasabah - Hubungan dengan bank" terisi sandi 2</message> 
      <message xml:lang="id">(Record ID = ID_2) Base: "Status hubungan dengan Bank" harus terisi sandi 9 jika Kolom "Klasifikasi Nasabah - Hubungan dengan bank" terisi sandi 2</message> 
     </unsatisfied> 
    </assertion> 
    <assertion id="Base_CekLembagaNilaiPemeringkat"> 
     <satisfied count="0"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_CekNeracaMinNol"> 
     <satisfied count="0"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_CekNilaiAgunanMinNol"> 
     <satisfied count="0"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_CekPersentaseMaksimal100"> 
     <satisfied count="0"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_CekLembaga-Tanggal-NilaiPeringkat"> 
     <satisfied count="0"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_CekIdentifier"> 
     <satisfied count="1"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_CekNomor-JumlahAgunan"> 
     <satisfied count="0"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_CekEligibilityJenisAgunan"> 
     <satisfied count="0"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_CekSifat-GolonganPenerbitAgunan"> 
     <satisfied count="0"/> 
     <unsatisfied count="0"/> 
    </assertion> 
    <assertion id="Base_cekPeriod"> 
     <satisfied count="1"/> 
     <unsatisfied count="0"/> 
    </assertion> 
</assertions> 

我怎樣才能得到的<assertion id="SLM_CekNonNegative">這是SLM_CekNonNegative價值?

下面是我到目前爲止的代碼:

string pathasr = @"D:\HARPITNAS\dari mbak julia\logs\123456789-2012-09-30-BSMS10.xml-assertions2.xml"; 
List<string> msg = new List<string>(); 
XmlDocument doc = new XmlDocument(); 
doc.Load(pathasr); 

foreach (XmlNode node in doc.DocumentElement) 
{ 
    string thevaluethatiwantvariable = string.Empty; 
    thevaluethatiwantvariable = ? ; 

    if (node.LastChild.HasChildNodes == true) 
    { 
      string count = node.LastChild.ChildNodes.Count.ToString(); 
      foreach (XmlNode y in node.LastChild.ChildNodes) 
      { 
       string masg = y.InnerXml; 
       msg.Add(masg); 
      } 
    } 
} 

此代碼不會得到我想要的價值。當我調試它時,我可以看到它沒有得到正確的值。有可能獲得我想要的價值嗎?

回答

4
thevaluethatiwantvariable = doc.DocumentElement.SelectSingleNode("//assertion[@name = 'id']/@value").Value; 

欲瞭解更多信息: http://www.w3schools.com/xpath/xpath_syntax.asp

+0

我已經按照你的解決方案,但我得到的錯誤'對象引用爲空' – Sabilv 2013-03-15 14:32:50

+0

如果你沒有對應的_SINGLE_節點,就會發生壞事。修正示例數據以反映您的真實情況。 – BlackICE 2013-03-15 16:32:33

+0

我已經修復了示例XML,爲您的預先 – Sabilv 2013-03-18 03:39:01

2

而不使用XPath這將是這樣的:

string theValue = doc.DocumentElement["assertion"].Attributes["id"].Value; 

而且也沒有必要對任何類型的循環。

+0

我需要foreach循環,因爲元素不僅僅是一個其中一些,如果我使用你的代碼,它只給出第一個元素值...? – Sabilv 2013-03-15 14:28:40

+1

@SabilValdano那麼你的例子是不完整的。 XPath解決方案也將只給你一個字符串。給我們完整的例子。 – Dialecticus 2013-03-15 16:28:26

+0

我已經修復了XML示例thx ... – Sabilv 2013-03-18 03:39:30