2012-02-13 84 views
1

我已經在我的字符串得到了下面的XML,我使用C#2.0如何檢查節點存在於XML和閱讀CDATA值

string strXML ="<?xml version="1.0"?> 
<tcm:Error xmlns:tcm="http://www.tridion.com/ContentManager/5.0" ErrorCode="80040329" Category="17" Source="Kernel" Severity="2"> 
    <tcm:Line ErrorCode="80040329" Cause="false" MessageID="16137"> 
    <![CDATA[Unable to save Keyword (tcm:0-0-0).]]><tcm:Token>RESID_4574</tcm:Token><tcm:Token>RESID_15309</tcm:Token><tcm:Token>tcm:0-0-0</tcm:Token> 
    </tcm:Line> 
    <tcm:Line ErrorCode="80040329" Cause="true" MessageID="15200"> 
    <![CDATA[Name must be unique for items of type: Keyword within this Category and its BluePrint context. Source or sources of conflict: tcm:236-215788-1024,tcm:237-215788-1024,tcm:241-215788-1024,tcm:243-215788-1024,tcm:423-215788-1024.]]><tcm:Token>RESID_15214</tcm:Token><tcm:Token>RESID_15309</tcm:Token><tcm:Token>RESID_15293</tcm:Token><tcm:Token>tcm:236-215788-1024,tcm:237-215788-1024,tcm:241-215788-1024,tcm:243-215788-1024,tcm:423-215788-1024</tcm:Token> 
    </tcm:Line> 
    <tcm:Details> 
    <tcm:CallStack> 
     <tcm:Location>UtilitiesBL.AssertUniqueTitle</tcm:Location> 
     <tcm:Location>UtilitiesBL.AssertUniqueTitle</tcm:Location> 
     <tcm:Location>KeywordBL.Create</tcm:Location> 
     <tcm:Location>XMLState.Save</tcm:Location> 
     <tcm:Location>Keyword.Save</tcm:Location> 
    </tcm:CallStack> 
    </tcm:Details> 
</tcm:Error>" 

現在我想要寫一個函數,它會返回字符串值首先檢查字符串strXML中是否有xml節點節點,如果沒有這樣的節點,則返回「valid」,否則我的函數將返回從上面的xml中獲取的字符串值。

所以我的返回結果將是「無法保存關鍵字(tcm:0-0-0)。名稱必須是唯一的類型的項目:關鍵字在此類別及其BluePrint上下文衝突源: tcm:236-215788-1024,tcm:237-215788-1024,tcm:241-215788-1024,tcm:243-215788-1024,tcm:423-215788-1024。「,這些文本以XML存在。

請建議!!

謝謝。

MS

回答

0

用途:

XmlDocument xml = new XmlDocument(); 
xml.LoadXml(strXML); 

XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xml.NameTable); 
xmlnsManager.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0"); 

XmlNodeList res = xml.SelectNodes("//tcm:Line/text()", xmlnsManager); 


foreach (XmlNode item in res) 
{ 
    Console.WriteLine(item.InnerText); 
} 
+0

好,謝謝基里爾,你能不能也請建議如何檢查特定節點的字符串存在加載到XmlDocument的面前。由於我的strXML可以是xml字符串以及普通字符串取決於返回什麼類型的結果,我需要使用.contains還是有其他方式 – 2012-02-13 10:53:03

+0

@MS,我建議你檢查'res.Count'屬性,例如:'if(res.Count == 0){...}'。它比用'Contains'檢查字符串要好。 – 2012-02-13 11:03:46

+0

但我們如何才能獲得(res.Count == 0)上傳整個字符串到XmlDocument之前,請建議! – 2012-02-13 11:06:04