XDocument 后代抛出空引用
XDocument descendants is throwing a null reference
我有一个这样的 XML 文档:
<?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>
我正在尝试像这样获取第一个 "ToolTipVariables" 元素:
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();
但它在尝试获取 FirstOrDefault 时一直抛出空引用异常。也许我在某个地方犯了一个简单的错误,但我找不到它。如有任何帮助,我们将不胜感激!
问题是您正在选择所有名为 Settings
的元素(实际上是根节点。)代码 document.Descendants("Settings")
将为您提供元素名称为的文档的所有后代Settings
。我认为您真正想要的是所有 Setting
元素。这将起作用:
XElement element = document.Descendants("Setting")
.Where(x => x.Attribute("name").Value == "ToolTipVariables")
.FirstOrDefault();
我有一个这样的 XML 文档:
<?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>
我正在尝试像这样获取第一个 "ToolTipVariables" 元素:
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();
但它在尝试获取 FirstOrDefault 时一直抛出空引用异常。也许我在某个地方犯了一个简单的错误,但我找不到它。如有任何帮助,我们将不胜感激!
问题是您正在选择所有名为 Settings
的元素(实际上是根节点。)代码 document.Descendants("Settings")
将为您提供元素名称为的文档的所有后代Settings
。我认为您真正想要的是所有 Setting
元素。这将起作用:
XElement element = document.Descendants("Setting")
.Where(x => x.Attribute("name").Value == "ToolTipVariables")
.FirstOrDefault();