C# Linq to XML check if element exists

前端 未结 3 1235
傲寒
傲寒 2020-12-06 04:25

I have an XML document as follows:


 
   \"+447528349828\" 
   \"09/06/24 
          


        
3条回答
  •  臣服心动
    2020-12-06 05:11

    Assuming that you have your number in some canonicalized form and your XML is loaded into an XmlDocument or some such, the simplest non-LINQ way to do it is with an XPath query:

    string pattern = String.Format("/Database/SMS/Number[. = '{0}']", number);
    if (myDoc.SelectSingleNode(pattern) != null)
    {
       // number already exists in document
    }
    

提交回复
热议问题