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

How to create a XML file in azure storage?
Wrong returnURL with Helicon isapi re-write + asp.net LoginStatus
How to convert more than one GridView to one XML file
How to test that the list Count is a certain value?
Can a Class be Self-Referential?
Implicit cast on reference type
I have an Excel Template And I have Edit It from c# Save new File
WPF Combo box collection container displayed text
Code First Foreign Key error in Dependent and Principal Roles
ASP.net many to many relation
Sql XML to Delphi
Show coordinates (or ruler) on my Canvas
Using Multi-core (-thread) processor for FOR loop
Most efficient way of querying secondary index in redis
Deserializing Import Io JSON with multiple objects
hide and unhide context strip menu item c#

Categories

HOME
c#
actionscript-3
internet-explorer
http
javafx
optimization
itunesconnect
phantomjs
azure-database-mysql
livecharts
magento-1.9.2.1
watson-iot
mailchimp
richtextbox
sequence
python-3.4
rmarkdown
virtualhost
google-api-oauth
pyodbc
segmentation-fault
cracking
dynamics-crm-2011
drush
saucelabs
aggregate
pylint
project-structure
git-branch
gsuite
android-xml
drombler-fx
semantic-ui-react
paper-trail-gem
xlib
urhosharp
eclipse-jdt
traitsui
openvms
android-geofence
realloc
jslider
mixed-models
directadmin
uitextview
smooth-streaming
grails-2.0
npoco
stanford-nlp-server
history
cloudconvert
standard-error
e4
visualizer
xamarin-test-cloud
liferay-ide
reloaddata
msiexec
gevent
firebase-job-dispatcher
toggleclass
scikit-image
xcode7.1
init
airwatch
django-taggit
commercetools
self-join
azure-mobile-app
playn
log-analysis
tidesdk
beagleboard
data-quality-services
mathics
optimistic-locking
shunting-yard
information-hiding
formbuilder
sat4j
telerik-appbuilder
power-management
xml-validation
qgraphicsscene
mandelbrot
jython-2.5
asp.net-4.0
removeall
windows-scheduler
nsregularexpression
orchestration
ios6-maps
systrace
socketasynceventargs
peoplepicker
jqtransform
conditional-operator
chars
subdirectories
web-widget
scala-designer
httpbrowsercapabilities

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