2013-11-02 156 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

相關問題