2011-04-15 115 views
0

我想弄清楚第一次Xmldocument。我無法弄清楚如何從XML文件中提取單個元素。我需要在xml文檔中找到某個URL,並確定用戶是否有權訪問該URL並刪除與該文章相關的xml(如果用戶沒有)。如何從此XML的特定元素中檢索文本?

我需要知道如何從<url></url>在下面的XML

<?xml version="1.0" encoding="UTF-8"?> 
<searchdoc><results hits="3226" time="0.33" query="test" suggest="test1" filter=""  sort="relevance" sortdir="desc" start="1" end="10" currentpage="1" lastpage="323" startdate="0" enddate="0" xsl="Trane.xsl"> 
<result no="1"><url>c:\RECYCLER\S-1-5-21-3289705215-1832128825-2807327032-470872\Dc115 
\test-files\test.ppt</url><col>3</col><lastmodified>25 Feb 2011 20:14:41  
GMT</lastmodified><indexdate>11 Mar 2011 20:40:17 GMT</indexdate><size>75264</size> 
<title><highlight>Test</highlight></title><alpha>Test</alpha><keywords 
/><contenttype>PPT</contenttype><context>Nutch Parser <highlight>Test</highlight> My 
initial <highlight>test</highlight> file for the PowerPoint parser of nutch Second 
page <highlight>Test</highlight> Of PowerPoint Extraction Some Unicode I do not know 
the content and I can not read it, just gathered from other ppt-files: áéíóú Stephan 
Strittmatter</context><description>Nutch Parser <highlight>Test</highlight> My initial 
<highlight>test</highlight> file for the PowerPoint parser of nutch ...</description> 
<language>en</language><score>100</score></result> 

很抱歉的凌亂XML我直接從存儲它的字符串拉動拉內部文本。我離開了其餘的xml,因爲它非常長。我將如何訪問我想要的信息?

+1

請在XPath和一些閱讀的Xm​​lDocument – tomasmcguinness 2011-04-15 19:44:45

+0

的selectSingleNode方法這有助於謝謝! – Christopher 2011-04-15 19:47:48

+1

嘗試docXml.DocumentElement? – tomasmcguinness 2011-04-15 19:49:47

回答

3

使用LINQ的的XDocument:

;從我的頭頂

,語法可能是有點偏離

XDocument doc = XDocument.Parse(yourString); 

string url = (from x in doc.Descendants("url") select x.Value).FirstOrDefault(); 
相關問題