Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询XML文件_C#_Xml_Ravendb - Fatal编程技术网

C# 查询XML文件

C# 查询XML文件,c#,xml,ravendb,C#,Xml,Ravendb,早上好 我正在尝试创建一个应用程序,它将接受一个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>

早上好 我正在尝试创建一个应用程序,它将接受一个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文件中找到所有标记名,并动态地将它们放在SN、
ICName
TotalCost
的位置。 这是因为我想给我所有标签的名称和它的值,在它旁边,而不仅仅是其中的一些

我尝试了以下方法:

XMLFile.Element(Name) = fund_mech.Element(fund_mech.Name).Value
{ SN = 1769, ICName = NCI, TotalCost = 2824 }
{ SN = 1679, ICName = NCST, TotalCost = 1191 }
但它不起作用

当前文件的输出如下所示:

XMLFile.Element(Name) = fund_mech.Element(fund_mech.Name).Value
{ SN = 1769, ICName = NCI, TotalCost = 2824 }
{ SN = 1679, ICName = NCST, TotalCost = 1191 }
但是,在xml文件中还有更多的标记,我希望以最佳方式检索这些标记


有人能帮忙吗?

我不知道您的最终目标是什么,但您可以使用xml填充ExpandoObject

从MSDN:

“表示可以在运行时动态添加和删除其成员的对象。”

var x=newexpandoobject()作为IDictionary;
string fm=“培训,个人”;
var name=来自XMLFile.Elements(“项目”).Elements(“行”)中的信息
其中info.Element(“融资机制”).Value==fm
选择信息;
x=GetExpandoForNodes(names.subjects());
foreach(x.Keys中的字符串键)
{
WriteLine(“Key:{0},Value:{1}”,Key,x[Key]);
}
Console.ReadKey();
下面的递归代码是对上的代码的轻微修改。在将元素名称保存为键之前,对其进行了更改,以将其转换为标题大小写并删除下划线。此外,如果有多个子元素具有相同的名称,则将值保存到字符串的通用列表中

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;
    }
私有静态动态GetExpandoForNodes(IEnumerable节点)
{            
var config=new ExpandoObject()作为IDictionary;
foreach(节点中的元素n)
{
如果(n.子体().Count()==0)
{
//通过转换为标题大小写并删除下划线,从xml标记创建属性名称
string propName=System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(n.Name.ToString().ToLower()).Replace(““,”);
var nodecount=来自节点中的c
其中c.Name==n.Name
选择c;
如果(nodecount.Count()>1)
{
List lst=新列表();
if((配置为IDictionary.ContainsKey(propName))
{                            
lst=(列表)(配置为IDictionary)[propName];
lst.Add(n.Value.Trim());
(配置为IDictionary)[propName]=lst;
}
其他的
{
(配置为IDictionary)[propName]=new List(){n.Value.Trim()};
}
}
其他的
{
(配置为IDictionary)[propName]=n.Value.Trim();
}              
}
其他的
{
动态子节点=GetExpandoForNodes(n.后代());
}
}
返回配置;
}

你能发布完整的XML或至少完整的XML部分吗?你的
where
语句将根据
fm
的值进行过滤,因此如果你想从整个XML文件中删除所有元素,你可能会删除它。我只想检索fm=“”。。。但我希望能够一次检索这些元素的所有标记及其值,而不必一次定义每个元素,例如序列号、总成本等。。。您可以使用从XML创建poco,这可能比您当前的方法更简单。您可以使用从XML生成一个类,这样可以节省所有键入的时间。