Check if XML Element exists

后端 未结 13 1664
傲寒
傲寒 2020-12-03 06:20

How can someone validate that a specific element exists in an XML file? Say I have an ever changing XML file and I need to verify every element exists before reading/parsing

13条回答
  •  被撕碎了的回忆
    2020-12-03 07:13

    //if the problem is "just" to verify that the element exist in the xml-file before you //extract the value you could do like this

    XmlNodeList YOURTEMPVARIABLE = doc.GetElementsByTagName("YOUR_ELEMENTNAME");
    
            if (YOURTEMPVARIABLE.Count > 0 )
            {
                doctype = YOURTEMPVARIABLE[0].InnerXml;
    
            }
            else
            {
                doctype = "";
            }
    

提交回复
热议问题