如何使用c#2.0将ArrayList字符串值与XDocument Title属性匹配

时间:2012-02-02 20:03:07

标签: xml c#-2.0

我的XML格式字符串如下所示:

<?xml version="1.0"?>
<tcm:ListKeywords xmlns:tcm="http://www.tridion.com/ContentManager/5.0" Managed="1024">
  <tcm:Item ID="tcm:229-552514-1024" Type="1024" Title="Aalborg" Lock="0" IsRoot="true"/>
  <tcm:Item ID="tcm:229-552512-1024" Type="1024" Title="Aarhus" Lock="0" IsRoot="true"/>
  <tcm:Item ID="tcm:229-329379-1024" Type="1024" Title="Aberdeen" Lock="0" IsRoot="true"/>
  <tcm:Item ID="tcm:229-569711-1024" Type="1024" Title="Abha" Lock="0" IsRoot="true"/>
  <tcm:Item ID="tcm:229-192866-1024" Type="1024" Title="Abidjan" Lock="0" IsRoot="true"/>
  <tcm:Item ID="tcm:229-569704-1024" Type="1024" Title="Abilene" Lock="0" IsRoot="true"/>
  <tcm:Item ID="tcm:229-192850-1024" Type="1024" Title="Abu Dhabi" Lock="0" IsRoot="true"/>
  <tcm:Item ID="tcm:229-192888-1024" Type="1024" Title="Accra" Lock="0" IsRoot="true"/>
</tcm:ListKeywords>

现在我有字符串的Arraylist,我想编写一个函数,它将上面的XML字符串作为参数以及我的Arraylist字符串,并将与XML // Item Title属性匹配,例如如下:

public bool matchArrayWithXMLTitle(Xmldocument xDoc, string str)
{
    If (//Item/Title == str)
    return true;
    else
    return false;
} 

然后我将使用它如下面的代码

bool matchStr = matchArrayWithXMLTitle(xDoc,"Abidjan"); //当Abidjan存在于XML中时,应该返回true

请建议!!

1 个答案:

答案 0 :(得分:0)

以下解决方案适合我。

static void Main(string[] args)
        {

            string xmlListKeywords = @"<?xml version=""1.0""?>
                    <tcm:ListKeywords xmlns:tcm=""http://www.tridion.com/ContentManager/5.0"" Managed=""1024"">
                      <tcm:Item ID=""tcm:229-552514-1024"" Type=""1024"" Title=""Aalborg"" Lock=""0"" IsRoot=""true""/>
                      <tcm:Item ID=""tcm:229-552512-1024"" Type=""1024"" Title=""Aarhus"" Lock=""0"" IsRoot=""true""/>
                      <tcm:Item ID=""tcm:229-329379-1024"" Type=""1024"" Title=""Aberdeen"" Lock=""0"" IsRoot=""true""/>
                      <tcm:Item ID=""tcm:229-569711-1024"" Type=""1024"" Title=""Abha"" Lock=""0"" IsRoot=""true""/>
                      <tcm:Item ID=""tcm:229-192866-1024"" Type=""1024"" Title=""Abidjan"" Lock=""0"" IsRoot=""true""/>
                      <tcm:Item ID=""tcm:229-569704-1024"" Type=""1024"" Title=""Abilene"" Lock=""0"" IsRoot=""true""/>
                      <tcm:Item ID=""tcm:229-192850-1024"" Type=""1024"" Title=""Abu Dhabi"" Lock=""0"" IsRoot=""true""/>
                      <tcm:Item ID=""tcm:229-192888-1024"" Type=""1024"" Title=""Accra"" Lock=""0"" IsRoot=""true""/>
                    </tcm:ListKeywords>";
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(xmlListKeywords);

            bool bMatch = matchArrayWithXMLTitle(xdoc, "Abidjan");
            bMatch = matchArrayWithXMLTitle(xdoc, "Test");

        }

        public static bool matchArrayWithXMLTitle(XmlDocument xDoc, string str)
        {
            XmlNode targetNode = xDoc.SelectSingleNode("//*[@Title = '" + str + "']");
            if (targetNode != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

请提出任何问题。

感谢。

M.S