C# 同名的命名空间节点-所需属性的内容
获得此文档:C# 同名的命名空间节点-所需属性的内容,c#,xml,C#,Xml,获得此文档: <uniprot xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/ XMLSchema-instance" xsi:schemaLocation="http://uniprot.org/uniprot http://www.uniprot.org/support/docs/uniprot.xsd"> <entry dataset="Swiss-Prot" created
<uniprot xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/ XMLSchema-instance" xsi:schemaLocation="http://uniprot.org/uniprot http://www.uniprot.org/support/docs/uniprot.xsd">
<entry dataset="Swiss-Prot" created="1986-07-21" modified="2013-10-16" version="88">
<dbReference type="GO" id="GO:0006412">
<property type="term" value="P:translation"/>
<property type="evidence" value="IEA:InterPro"/>
</dbReference>
<dbReference type="HAMAP" id="MF_00294">
<property type="entry name" value="Ribosomal_L33"/>
<property type="match status" value="1"/>
</dbReference>
<dbReference type="InterPro" id="IPR001705">
<property type="entry name" value="Ribosomal_L33"/>
</dbReference>
。。。我需要获取类型的属性以及类型的内容是否为GO,如果是,则获取该确切节点的以下数据,即id和value。几个小时来我一直在思考这个问题,在谷歌上搜索这个问题,但我只是缺乏知识 我建议使用,我发现它比XmlDocument和XPath查询容易得多,但这至少部分是个人偏好
我不太清楚你所说的每个元素的“值”是什么意思,带有“GO”的类型,但这应该可以让你大致理解goTypeNodes
将包含一组具有“GO”类型及其ID和类型值的节点,并且还包含它们下面的属性元素,因此如果“value”指的是它们下面的属性元素的值,那么从那里获取这些值就很简单了
XNamespace ns = "http://uniprot.org/uniprot";
XDocument doc = XDocument.Load(@"C:\SO\Foo.xml");
var goTypeNodes = from n in doc.Descendants(ns + "dbReference")
select new { Id = n.Attribute("id").Value, Type = n.Attribute("type").Value, Properties = n.Elements()};
顺便说一句,您的示例XML缺少uniprot和entry的结束标记。实际上我设法解决了这个问题:
XmlNodeList Testi = XMLdoc.SelectNodes("//ns:dbReference", nsmgr);
foreach (XmlNode xn in Testi)
{
if (xn.Attributes["type"].Value == "GO")
{
String Testilator = xn.Attributes["id"].Value;
String Testilator2 = xn.FirstChild.Attributes["value"].Value;
}
}
当然是。我只删掉了那份文件的重要部分。这就是完整版本的样子:dbReference在其中大约有20次。我需要根据dbreference节点的属性来选择它。如果type属性设置为“GO”(通常有大约3-10个包含“GO”),我需要该节点的其余部分和子节点。或者更准确地说:如果dbReference type==“GO”,则获取该确切节点的dbReference id以及子节点属性的value属性的内容。
XmlNodeList Testi = XMLdoc.SelectNodes("//ns:dbReference", nsmgr);
foreach (XmlNode xn in Testi)
{
if (xn.Attributes["type"].Value == "GO")
{
String Testilator = xn.Attributes["id"].Value;
String Testilator2 = xn.FirstChild.Attributes["value"].Value;
}
}