2013-07-16 35 views
0

早上好 我正在嘗試創建一個應用程序,它將接受一個XML文件並將所選數據導入到Raven數據庫中。查詢XML文件

我有以下XML文件: 這是XML代碼

<?xml version="1.0" encoding="UTF-8"?> 
<PROJECTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<row> 
<APPLICATION_ID>8524889</APPLICATION_ID> 
<ACTIVITY>F30</ACTIVITY> 
<ADMINISTERING_IC>DK</ADMINISTERING_IC> 
<APPLICATION_TYPE>1</APPLICATION_TYPE> 
<ARRA_FUNDED>N</ARRA_FUNDED> 
<BUDGET_START>06/24/2013</BUDGET_START> 
<BUDGET_END>06/23/2014</BUDGET_END> 
<FOA_NUMBER>PA-11-110</FOA_NUMBER> 
<FULL_PROJECT_NUM>1F30DK099017-01</FULL_PROJECT_NUM> 
<FUNDING_ICs>NIDDK:32342\</FUNDING_ICs> 
<FY>2013</FY> 
<NIH_SPENDING_CATS xsi:nil="true"/> 
<ORG_CITY>STANFORD</ORG_CITY> 
<ORG_COUNTRY>UNITED STATES</ORG_COUNTRY> 
<ORG_DISTRICT>18</ORG_DISTRICT> 
<ORG_DUNS>009214214</ORG_DUNS> 
<ORG_DEPT>MICROBIOLOGY/IMMUN/VIROLOGY</ORG_DEPT> 
<ORG_FIPS>US</ORG_FIPS> 
<ORG_STATE>CA</ORG_STATE> 
<ORG_ZIPCODE>943056203</ORG_ZIPCODE> 
<IC_NAME>NATIONAL INSTITUTE OF DIABETES AND DIGESTIVE AND KIDNEY DISEASES</IC_NAME> 
<ORG_NAME>STANFORD UNIVERSITY</ORG_NAME> 
<PIS><PI><PI_NAME>PHAM, EDWARD ANHOA</PI_NAME><PI_ID>11461179</PI_ID></PI></PIS> 
<PROJECT_TERMSX><TERM>5&apos;Untranslated Regions</TERM><TERM>Acylation</TERM><TERM>Adverse effects</TERM><TERM>Affect</TERM><TERM>anti-hepatitis C</TERM><TERM>Antiviral Agents</TERM><TERM>base</TERM><TERM>Beryllium</TERM><TERM>Binding (Molecular Function)</TERM><TERM>Biological</TERM><TERM>Biological Process</TERM><TERM>Cell Culture Techniques</TERM><TERM>Cells</TERM><TERM>Crystallography</TERM><TERM>Data</TERM><TERM>design</TERM><TERM>effective therapy</TERM><TERM>Elements</TERM><TERM>fluorophore</TERM><TERM>Foundations</TERM><TERM>Future</TERM><TERM>Genetic Transcription</TERM><TERM>Genome</TERM><TERM>Genomics</TERM><TERM>Goals</TERM><TERM>Gold</TERM><TERM>Hepatitis C virus</TERM><TERM>Hydroxyl Radical</TERM><TERM>Indium</TERM><TERM>Induced Mutation</TERM><TERM>Infection</TERM><TERM>Information Storage</TERM><TERM>inhibitor/antagonist</TERM><TERM>Lead</TERM><TERM>Life Cycle Stages</TERM><TERM>Liver</TERM><TERM>liver transplantation</TERM><TERM>Malignant neoplasm of liver</TERM><TERM>Maps</TERM><TERM>Mediating</TERM><TERM>Methods</TERM><TERM>Microfluidics</TERM><TERM>MicroRNAs</TERM><TERM>Modeling</TERM><TERM>Molecular Virology</TERM><TERM>Monitor</TERM><TERM>mutant</TERM><TERM>Mutation</TERM><TERM>Names</TERM><TERM>next generation</TERM><TERM>novel</TERM><TERM>Nuclear Magnetic Resonance</TERM><TERM>Nucleic Acid Regulatory Sequences</TERM><TERM>Patients</TERM><TERM>Pegylated Interferon Alfa</TERM><TERM>Play</TERM><TERM>Polymerase</TERM><TERM>Primer Extension</TERM><TERM>Proteins</TERM><TERM>Quartz</TERM><TERM>Regulatory Element</TERM><TERM>Research</TERM><TERM>Resistance</TERM><TERM>RNA</TERM><TERM>RNA Binding</TERM><TERM>RNA chemical synthesis</TERM><TERM>RNA Viruses</TERM><TERM>Role</TERM><TERM>screening</TERM><TERM>Site</TERM><TERM>small molecule</TERM><TERM>standard of care</TERM><TERM>Structure</TERM><TERM>Testing</TERM><TERM>therapeutic target</TERM><TERM>Time</TERM><TERM>tool</TERM><TERM>Translations</TERM><TERM>United States</TERM><TERM>Viral</TERM><TERM>Viral Proteins</TERM><TERM>viral RNA</TERM><TERM>Virus</TERM><TERM>Virus Replication</TERM></PROJECT_TERMSX> 
<PROJECT_TITLE>Structure-function determination of the Hepatitis C Virus negative RNA strand???s</PROJECT_TITLE> 
<PROJECT_START>06/24/2013</PROJECT_START> 
<PROJECT_END>06/23/2016</PROJECT_END> 
<PHR> Hepatitis C virus (HCV) is the most common cause of liver cancer and the leading indication for liver transplantation in the United States, both of which are preventable by more effectively treating the etiologic infection. Current standard of care (SOC) for HCV treatment is inadequate for many patients and associated with significant toxic side effects. The goal of this project is to determine the relevant RNA secondary structures of a key RNA regulatory element in the HCV life cycle, which can form the basis for novel antiviral strategies directed at targeting key RNA elements.</PHR> 
<SERIAL_NUMBER>99017</SERIAL_NUMBER> 
<STUDY_SECTION>ZDK1</STUDY_SECTION> 
<STUDY_SECTION_NAME>Special Emphasis Panel</STUDY_SECTION_NAME> 
<SUPPORT_YEAR>1</SUPPORT_YEAR> 
<SUFFIX xsi:nil="true"/> 
<SUBPROJECT_ID xsi:nil="true"/> 
<TOTAL_COST>32342</TOTAL_COST> 
<TOTAL_COST_SUB_PROJECT xsi:nil="true"/> 
<CORE_PROJECT_NUM>F30DK099017</CORE_PROJECT_NUM> 
<CFDA_CODE>847</CFDA_CODE> 
<PROGRAM_OFFICER_NAME>PODSKALNY, JUDITH M,</PROGRAM_OFFICER_NAME> 
<ED_INST_TYPE>SCHOOLS OF MEDICINE</ED_INST_TYPE> 
<AWARD_NOTICE_DATE>04/19/2013</AWARD_NOTICE_DATE> 
<FUNDING_MECHANISM>Training, Individual</FUNDING_MECHANISM> 
</row> 
</PROJECTS> 

的樣品,在我查詢用下面的代碼的XML文件中的時刻:

string fm = "Some Text"; 
var names = from info in XMLFile.Descendants("row") 
      where info.Element("FUNDING_MECHANISM").Value == fm 
      select new { 
      // much more to be added... 
      SN=info.Element("SERIAL_NUMBER").Value, 
      ICName=info.Element("IC_NAME").Value, 
      TotalCost=info.Element("TOTAL_COST").Value }; 

的東西是我一般希望查詢從xml文件中找到所有標籤名稱,並動態地將它們放在SN的位置ICNameTotalCost。 這是因爲我想給我所有的標籤名稱和它的價值,它旁邊,而不僅僅是其中的一些。

我試過如下:

XMLFile.Element(Name) = fund_mech.Element(fund_mech.Name).Value 
查詢

,但它不工作。此刻的文件

臨屋輸出如下:

{ SN = 1769, ICName = NCI, TotalCost = 2824 } 
{ SN = 1679, ICName = NCST, TotalCost = 1191 } 

然而,也有,我想以最佳的方式檢索XML文件更多的標籤。

有人可以幫忙嗎?

+0

你可以發佈完整的XML或至少完整的一節嗎? – DGibbs

+0

而你的'where'語句將根據'fm'的值進行過濾,所以如果你想要所有元素,你可能會刪除這個。 – DGibbs

+0

從整個XML文件我只想檢索fm =「」的元素...但我希望能夠一次檢索這些元素的所有標籤及其值,而不必一次定義每個元素,例如SERIAL_NUMBER,TOTAL_COST等 –

回答

0

我不知道您的最終目標是什麼,但您可以使用xml來填充ExpandoObject。

從MSDN:

「表示一個對象,其構件可被動態地添加和在運行時除去。」

 var x = new ExpandoObject() as IDictionary<string, Object>; 
     string fm = "Training, Individual"; 

     var names = from info in XMLFile.Elements("PROJECTS").Elements("row") 
        where info.Element("FUNDING_MECHANISM").Value == fm 
        select info; 

     x = GetExpandoForNodes(names.Descendants()); 

     foreach (string key in x.Keys) 
     { 
      Console.WriteLine("Key: {0}, Value: {1}", key, x[key]); 
     } 
     Console.ReadKey(); 

下面的遞歸代碼是在https://gist.github.com/asoftwareguy/1064812找到的代碼的一個輕微的修改。在將其保存爲關鍵字之前,進行了更改以將元素名稱轉換爲標題大小寫併除去下劃線。而且,如果有多個具有相同名稱的子元素,則這些值將保存到字符串的通用列表中。

private static dynamic GetExpandoForNodes(IEnumerable<XElement> nodes) 
    {    
     var config = new ExpandoObject() as IDictionary<string, Object>; 
     foreach (XElement n in nodes) 
     { 
      if (n.Descendants().Count() == 0) 
      { 

       //create your property name from the xml tags, by converting to title-case and removing the underscore 
       string propName = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(n.Name.ToString().ToLower()).Replace("_", ""); 

       var nodecount = from c in nodes 
           where c.Name == n.Name 
           select c;      

       if (nodecount.Count() > 1) 
       { 
        List<string> lst = new List<string>(); 
        if ((config as IDictionary<String, object>).ContainsKey(propName)) 
        {        
         lst = (List<string>)(config as IDictionary<String, object>)[propName]; 
         lst.Add(n.Value.Trim()); 
         (config as IDictionary<String, object>)[propName] = lst; 
        } 
        else 
        { 
         (config as IDictionary<String, object>)[propName] = new List<string>() { n.Value.Trim() }; 
        } 

       } 
       else 
       { 
        (config as IDictionary<String, object>)[propName] = n.Value.Trim(); 
       }    
      } 
      else 
      { 
       dynamic child = GetExpandoForNodes(n.Descendants());           
      } 
     } 
     return config; 
    }