2013-11-02 70 views
0

属性的内容得到了这份文件:命名空间节点 - 需要

<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> 

现在,我使用这个抢节点的内部文本,它工作得很好...但是。 ..

XmlDocument XMLdoc = new XmlDocument(); 
XMLdoc.Load(Datapath); 
XmlNamespaceManager nsmgr = new XmlNamespaceManager(XMLdoc.NameTable); 
nsmgr.AddNamespace("ns", "http://uniprot.org/uniprot"); 
String NodeName = XMLdoc.SelectSingleNode("//ns:fullName", nsmgr).InnerText; 

...我需要抓住的是否类型的内容是去还是不去,如果是,拿到确切的节点,即ID和值的以下数据的属性。一直在思考和搜索几个小时,我只是缺乏知识去任何地方。

回答

0

其实我设法解决这个问题:

  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; 

       } 
      } 
0

我建议使用Linq to Xml,我发现它比XmlDocument和XPath查询更容易,但这至少部分是个人偏好。

我不太确定每个元素的“值”是什么意思,类型是“GO”,但是这应该会让您获得最大的路径。 goTypeNodes将包含这些节点的集合,这些节点具有带有其ID和类型值的“GO”类型,并且还包含其下的属性元素,因此如果“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和条目的结束标记。

+0

当然是。我只删除了该文件的重要部分。这就是完整的样子:http://www.uniprot.org/uniprot/P30178.xml dbReference在那里有20次左右。我需要通过其属性挑选出dbReferene节点。如果type属性设置为“GO”(通常大约有3-10个包含“GO”),我需要该节点的其余部分和子节点。或者更确切地说:如果dbReference type ==“GO”获得该确切节点的dbReference id和子节点属性的value属性的内容。 – MeepMania

相关问题