c#


XDocument descendants is throwing a null reference


I've got an XML document like this:
<?xml version="1.0" encoding="utf-8"?>
<Settings updated="3/21/2017 15:48">
<Setting name="ToolTipVariables"><?xml version="1.0" encoding="utf-16"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Title</string>
<string>Date</string>
<string>Description</string>
<string>Location</string>
<string>Id</string>
</ArrayOfString></Setting>
<Setting name="ToolTipVariables"><?xml version="1.0" encoding="utf-16"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Title</string>
<string>Date</string>
<string>Description</string>
<string>Location</string>
<string>Id</string>
</ArrayOfString></Setting>
<Setting name="ToolTipVariables"><?xml version="1.0" encoding="utf-16"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Title</string>
<string>Date</string>
<string>Description</string>
<string>Location</string>
<string>Id</string>
</ArrayOfString></Setting>
<Setting name="ShowUpdateWindow"><?xml version="1.0" encoding="utf-16"?>
<boolean>true</boolean></Setting>
</Settings>
And I'm trying to grab the first "ToolTipVariables" element like this:
XDocument document = XDocument.Load(settingsPath); //settingsPath is where the XML Document is located
XElement element = document.Descendants("Settings").Where(x => x.Attribute("name").Value == "ToolTipVariables").FirstOrDefault();
but it keeps throwing a null reference exception when trying to get the FirstOrDefault. Maybe I'm making a simple mistake somewhere, but I can't find it. Any help would be appreciated!
The problem is that you are selecting all elements called Settings (where that is actually the root node.) The code document.Descendants("Settings") will give you all descendants of the document where the element name is Settings. I think what you really want is all Setting elements. This will work:
XElement element = document.Descendants("Setting")
.Where(x => x.Attribute("name").Value == "ToolTipVariables")
.FirstOrDefault();

Related Links

New value on gridview edit mode
The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument
c# How to programmatically disable Windows 8 specific features
Passing carriage return to SQL SP through C#
Find an ID through having a username?
Saving raw json as a nested property to raven DB
Embed image in file
how can i get RouteValueDictionary values from Page.RouteData.DataTokens
WCF Ria Call Method And Error Not Found
match Email Regular expression from html string
Default Proxy IE
2100 hour + 1 minute as a timer.interval
pbkdf2 computation not consistent between C# and JavaScript
How to make a WPF listbox item not clickable
Orthodoxic notation/style of naming in C# [duplicate]
Breeze SaveChanges always throws DbUpdateConcurrencyException when deleting entity

Categories

HOME
qlikview
internet-explorer
kendo-ui-angular2
arguments
assign
sms
xss
snmp
thermal-printer
zabbix
rethinkdb
token
google-app-maker
virtualenv
mbed
web-testing
single-page-application
descriptor
bug-reporting
multipart
klipfolio
concatenation
angular-schema-form
meteor-accounts
restangular
federated
ios9
jboss-arquillian
drawrect
multilanguage
unity3d-5
wunderground
asp.net-webhooks
maven-2
mapquest
mybatis-generator
file-conversion
uitextview
rexx
togglebutton
serverless-architecture
google-chrome-storage
kendo-scheduler
blackberry
timex
pyautogui
cedar-bdd
firebase-job-dispatcher
magick.net
nsoperationqueue
datamodel
instabug
app-data
jain-sip
cyberduck
tess4j
eula
cakephp-2.6
waterline
parseexception
iphone-6
method-overloading
fedena
clearcanvas
sync
sat4j
drawable
pyalgotrade
sendy
php-amqplib
google-voice
gmail-imap
orchestration
esi
event-propagation
django-admin-tools
mvcmailer
jqtransform
hs-err
cpack
http-response-codes
calendarextender
ihtmldocument2
asp.net-1.1

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App