Hi i am doing as follow
XDocument xmlDoc = XDocument.Load(@"F:\test2.xml");
var q = from c in xmlDoc.Descendants("autoivr.ok")
where c.Element("LS_CZIP4").Value == "1234"
select new
{
name = c.Element("LS_LIN").Value,
state = c.Element("LS_STATE").Value
};
When i use
where c.attribute(“LS_CZIP4”).Value == “1234”
i get error of object reference not set but when i use c.element there is no such error.
Following is the xml i made which is actually a table in sql converted to xml file
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<autoivr.ok>
<LS_LIN>abc</LS_LIN>
<LS_STATE>def</LS_STATE>
<LS_TYPE>5</LS_TYPE>
<LS_CZIP4>1234</LS_CZIP4>
<priority>0</priority>
</autoivr.ok>
Can someone let me know the problem and how can i resolve and can i work with element tag only instead of attribute . Thank You
Use casting instead of accessing
Valueproperty. Casting to string will return null for non-existing elements. GettingValuewill throw an exceptionBTW you need closing tag for
<DocumentElement>. AlsoLS_CZIP4is element, not attribute. See the difference here XML Elements vs. Attributes.Element:
<LS_LIN>abc</LS_LIN>Attribute:
<autoivr.ok LS_LIN="abc">