【问题标题】:How to match ArrayList string value with the XDocument Title attribute using c# 2.0如何使用 c# 2.0 将 ArrayList 字符串值与 XDocument Title 属性匹配
【发布时间】:2012-02-02 20:03:06
【问题描述】:

我得到了如下的 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"); //应该返回true,因为Abidjan在XML中

请推荐!!

【问题讨论】:

    标签: xml c#-2.0


    【解决方案1】:

    以下解决方案对我有用。

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

    如有任何问题请提出建议。

    谢谢。

    硕士

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多