2014-02-17 21 views
0

我有一個帶有嵌套元素的xml文件。我需要加載xml文件並將特定子元素的值保存到sql數據庫中。如何獲得c#asp.net中的xml父節點的子元素值(不使用linq)

XML文件:

<section id="A00-A09"> 
      <desc>Intestinal infectious diseases (A00-A09)</desc> 
      <diag> 
      <name>A00</name> 
      <desc>Cholera</desc> 
      <diag> 
       <name>A00.0</name> 
       <desc>Cholera due to Vibrio cholerae 01, biovar cholerae</desc> 
       <inclusionTerm> 
       <note>Classical cholera</note> 
       </inclusionTerm> 
      </diag> 
      <diag> 
       <name>A00.1</name> 
       <desc>Cholera due to Vibrio cholerae 01, biovar eltor</desc> 
       <inclusionTerm> 
       <note>Cholera eltor</note> 
       </inclusionTerm> 
      </diag> 
      <diag> 
       <name>A00.9</name> 
       <desc>Cholera, unspecified</desc> 
      </diag> 
      </diag> 
    </section> 
    <section id="A15-A19"> 
      <desc>Tuberculosis (A15-A19)</desc> 
      <includes> 
      <note>infections due to Mycobacterium tuberculosis and Mycobacterium bovis</note> 
      </includes> 
     <diag> 
      <name>A15</name> 
      <desc>Respiratory tuberculosis</desc> 
      <diag> 
       <name>A15.0</name> 
       <desc>Tuberculosis of lung</desc> 
       <inclusionTerm> 
       <note>Tuberculous bronchiectasis</note> 
       <note>Tuberculous fibrosis of lung</note> 
       <note>Tuberculous pneumonia</note> 
       <note>Tuberculous pneumothorax</note> 
       </inclusionTerm> 
      </diag> 
      <diag> 
       <name>A15.4</name> 
       <desc>Tuberculosis of intrathoracic lymph nodes</desc> 
       <inclusionTerm> 
       <note>Tuberculosis of hilar lymph nodes</note> 
       <note>Tuberculosis of mediastinal lymph nodes</note> 
       <note>Tuberculosis of tracheobronchial lymph nodes</note> 
       </inclusionTerm> 
       <excludes1> 
       <note>tuberculosis s`enter code here`pecified as primary (A15.7)</note> 
       </excludes1> 
      </diag> 
    </diag> 
    </section> 

我已經加載的XML,但不能因爲它是有相同的標籤名從子元素獲取值。是否有任何人能幫助我解決這個問題

+0

任何具體的原因,你想這樣做沒有linq?我認爲沒有linq這個任務會更困難,尤其是因爲嵌套的元素具有相同的名稱。請發佈一些你已經嘗試過的相關代碼,即使它失敗了。 – har07

+0

好的,你可以給我一個解決方案linq – Varghese

+0

你的XML文件無效張貼(它有多個根元素)。這是實際的格式嗎? *是的,這是實際的格式*。那麼我們不能在這種情況下使用linq,因爲文件不能被加載爲'XDocument'對象。否則,如果xml文件有一個根並且有效,那麼我們有機會使用linq,並且這種情況下的問題是從哪個特定元素中獲取值? – har07

回答

1

以下控制檯應用程序示例演示了一種方式來獲得<desc>元素是<section>元素的直接子,也拿到<name><desc>每個<diag>元素。我從System.Xml.Linq使用XDocument。但不使用LINQ查詢語法,因爲它是不是在這種情況下,必要的,你好像不喜歡它:

//load xml string 
var doc = XDocument.Parse(xml); 
//or use XDocument.Load("path_to_xml_file.xml"); to load from file 

//get all <section> element 
var sections = doc.Root.Elements("section"); 
//get <desc> and all <diag> under each <section> 
foreach (var section in sections) 
{ 
    var desc = (string)section.Element("desc"); 
    Console.WriteLine(String.Format("new Section. Desc: {0}", desc)); 
    var diags = section.Descendants("diag"); 
    foreach (var diag in diags) 
    { 
     var name = (string)diag.Element("name"); 
     var desc2 = (string)diag.Element("desc"); 
     Console.WriteLine(String.Format("name: {0}, desc: {1}", name, desc2)); 
    } 
} 

通知,Descendants()用於獲取用於獲取當前元素和Element()的任何子只有直接當前元素的子元素。

相關問題