I have the following XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Project>
<Site Address="0" Connect="COM1,9600">
</Site>
</Project>
I am trying to get the value of ‘Connect’
I have this code:
var doc = XDocument.Load(xml);
var q = from x in doc.Root.Elements()
where x.Name.LocalName == "Connect"
select x;
ClientTB.Text = q.FirstOrDefault().ToString();
But when I run this I get the error Object reference not set to an instance of an object.
If I change the where statement to:
where x.Name.LocalName == "Site"
Then my text contains <Site Address="0" Connect="COM1,9600"></Site>
What do I need to do to get the value of Connect?
Alternative