locked
Linq to XML - Check if Element exists RRS feed

  • Question

  • User1409124252 posted

    I have seen some other solutions to this, but they don't fit what I am trying to do.  The XML has elements that may not be in the file each time it is posted.

    The trick is, the query is dependent upon a question value to get the answer value.  Here is the code:

                string otherphone = (from e in contact.Descendants("DataElement")
                                    where e.Element("QuestionName").Value == "other_phone"
                                    select (string)e.Element("Answer").Value).FirstOrDefault();
                otherphone = (!String.IsNullOrEmpty(otherphone)) ? otherphone.Replace("'", "''") : null;
    

    Under the "contact" collection, here are many elements named "DataElement", so I am querying to find the one where the elements QuestionName is "other_phone", then I get the Answer value.

    How can I make it work if this element doesn't exist in the file?

    Monday, May 19, 2014 10:10 AM

Answers

  • User1409124252 posted

    Thanks very much Kevin, but I found the answer was simply changing null to "".

    string otherphone = (from e in contact.Descendants("DataElement")
                                    where e.Element("QuestionName").Value == "other_phone"
                                    select (string)e.Element("Answer").Value).FirstOrDefault();
                otherphone = (!String.IsNullOrEmpty(otherphone)) ? otherphone.Replace("'", "''") : "";
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 22, 2014 12:00 PM

All replies

  • User1918509225 posted

    Hi indyitman,

    For your post ,here are my suggestion:

    First ,get all the elements  whose name is DataElement,just like below:

    IEnumerable<XElement> q = from ele in ex.Elements("DataElement")
                               Select ele;
    

    Seconde iterate the elements founded above,and find if its chid element has a  element named “QuestionName”,

    if it has,get the value,if not have ,just skip the loop,just like code below:

                string val="";
                foreach (XElement x in q.Elements()) {
                   
                    if (x.Name == "QuestionName") {
                        val = x.Value;
                    }
    
                }
    

    If you have more than one element “QuestionName”,you can try to add the value into a List<string>,just like below:

     List<string> list=new List<string>();
     foreach (XElement x in q.Elements()) {
                   
                    if (x.Name == "QuestionName") {
                        list.Add(x.Value);
                    }
    
                }
    

    Hope it can help you.

    Monday, May 19, 2014 10:13 PM
  • User1409124252 posted

    Thanks very much Kevin, but I found the answer was simply changing null to "".

    string otherphone = (from e in contact.Descendants("DataElement")
                                    where e.Element("QuestionName").Value == "other_phone"
                                    select (string)e.Element("Answer").Value).FirstOrDefault();
                otherphone = (!String.IsNullOrEmpty(otherphone)) ? otherphone.Replace("'", "''") : "";
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 22, 2014 12:00 PM