locked
Linq to XML RRS feed

  • Question

  • User136806914 posted

    Does Linq throw an object reference exception when parent node doesn't exits, if yes how do we check if the parent node exist

    var test = from y in x.Descendants("test").Elements("test2")
               select......

    what will happen if test node doesn't exist?

    Thursday, November 9, 2017 4:02 PM

Answers

  • User1120430333 posted

    Does Linq throw an object reference exception when parent node doesn't exits, if yes how do we check if the parent node exist

    var test = from y in x.Descendants("test").Elements("test2")
               select......

    what will happen if test node doesn't exist?

    In this case, test,  the results and a collection, Test.Count will = 0, which means no items are in the collection.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 9, 2017 11:28 PM
  • User991499041 posted

    Hi PraveenAmar,

    Does Linq throw an object reference exception when parent node doesn't exits, if yes how do we check if the parent node exist

    var test = from y in x.Descendants("test").Elements("test2")
               select......

    what will happen if test node doesn't exist?

    In this case, if "test" node doesn't exist, it would not throw an exception.

    However if there is no Element and Attribute, it would throw NullReferenceException.

    Besides, if you want to know how to handle blank or no Element and Attribute, you could try this.

    XML file

    <?xml version="1.0" encoding="utf-8"?>
    <Employees>
      <Employee Location="Earth">
        <Name>Wriju</Name>
        <Email>a@a.com</Email>
      </Employee>
      <Employee Location="Moon">
        <Name>Tupur</Name>
        <Email>a@b.com</Email>
      </Employee>
      <Employee>
        <Name>Wrishika</Name>
      </Employee>  
    </Employees>

    var xml = XElement.Load(@"C:\xxx\xxx\Employee.xml");
    
    
    var q = from e in xml.Descendants("Employee")
           select new
           {
                Name = e.Element("Name").ElementValueNull(),
                Location = e.AttributeValueNull("Location"),
                Email = e.Element("Email").ElementValueNull()
           };
    foreach (var k in q)
    {
       Console.WriteLine("Name : {0}, Email : {1}, Location : {2}",
           k.Name, k.Email, k.Location);
    }
    
    
    //This method is to handle if element is missing
    public static string ElementValueNull(this XElement element)
    {
        if (element != null)
            return element.Value;
    
        return "";
    }
    
    //This method is to handle if attribute is missing
    public static string AttributeValueNull(this XElement element, string attributeName)
    {
        if (element == null)
            return "";
        else
        {
            XAttribute attr = element.Attribute(attributeName);
            return attr == null ? "" : attr.Value;
        }
    }

    Result

    Regards,

    zxj

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 10, 2017 3:13 AM

All replies

  • User1120430333 posted

    Does Linq throw an object reference exception when parent node doesn't exits, if yes how do we check if the parent node exist

    var test = from y in x.Descendants("test").Elements("test2")
               select......

    what will happen if test node doesn't exist?

    In this case, test,  the results and a collection, Test.Count will = 0, which means no items are in the collection.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 9, 2017 11:28 PM
  • User991499041 posted

    Hi PraveenAmar,

    Does Linq throw an object reference exception when parent node doesn't exits, if yes how do we check if the parent node exist

    var test = from y in x.Descendants("test").Elements("test2")
               select......

    what will happen if test node doesn't exist?

    In this case, if "test" node doesn't exist, it would not throw an exception.

    However if there is no Element and Attribute, it would throw NullReferenceException.

    Besides, if you want to know how to handle blank or no Element and Attribute, you could try this.

    XML file

    <?xml version="1.0" encoding="utf-8"?>
    <Employees>
      <Employee Location="Earth">
        <Name>Wriju</Name>
        <Email>a@a.com</Email>
      </Employee>
      <Employee Location="Moon">
        <Name>Tupur</Name>
        <Email>a@b.com</Email>
      </Employee>
      <Employee>
        <Name>Wrishika</Name>
      </Employee>  
    </Employees>

    var xml = XElement.Load(@"C:\xxx\xxx\Employee.xml");
    
    
    var q = from e in xml.Descendants("Employee")
           select new
           {
                Name = e.Element("Name").ElementValueNull(),
                Location = e.AttributeValueNull("Location"),
                Email = e.Element("Email").ElementValueNull()
           };
    foreach (var k in q)
    {
       Console.WriteLine("Name : {0}, Email : {1}, Location : {2}",
           k.Name, k.Email, k.Location);
    }
    
    
    //This method is to handle if element is missing
    public static string ElementValueNull(this XElement element)
    {
        if (element != null)
            return element.Value;
    
        return "";
    }
    
    //This method is to handle if attribute is missing
    public static string AttributeValueNull(this XElement element, string attributeName)
    {
        if (element == null)
            return "";
        else
        {
            XAttribute attr = element.Attribute(attributeName);
            return attr == null ? "" : attr.Value;
        }
    }

    Result

    Regards,

    zxj

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 10, 2017 3:13 AM