如何检查xml中是否存在节点,并通过读取CDATA值返回字符串值

本文关键字:CDATA 字符串 返回 节点 读取 存在 何检查 检查 是否 xml | 更新日期: 2023-09-27 18:24:23

我的字符串中有下面的xml,我使用的是c#2.0

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)。此类别及其蓝图上下文中的关键字类型的项的名称必须是唯一的。冲突源:tcm:236-215788-1024,tcm:237-215788-12024,tcm:241-215788-11024,tcm:243-215788-1027,tcl:423-215788-1022。",这些文本存在于XML中。

请建议!!

谢谢。

MS

如何检查xml中是否存在节点,并通过读取CDATA值返回字符串值

使用:

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);
}