0

早上好,我正在尝试创建一个可以接受 XML 文件并将选定数据导入 Raven 数据库的应用程序。

我有以下 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 文件中找到所有标签名称,并动态地将它们放在 SNICNameTotalCost. 这是因为我想给我所有标签的名称和它的值,在它旁边,而不仅仅是其中的一些。

我尝试了以下方法:

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

在查询中,但它不起作用。

目前该文件的输出如下:

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

但是,我想以最佳方式检索 xml 文件中的更多标签。

有人可以帮忙吗?

4

1 回答 1

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;
    }
于 2013-07-16T12:57:28.913 回答