从字符串XML中读取数据并启用节点false
问题描述:
我有一个xml字符串,我想从中读取数据。从字符串XML中读取数据并启用节点false
<?xml version="1.0" encoding="utf-16"?>
<Tree AllowNodeEditing="True" ShowLineImages="False" CheckBoxes="True"
EnableAjaxSkinRendering="False" AutoPostBackOnCheck="True" AutoPostBack="True">
<Node Enabled="False" Text="Geen afbeeldingen aanwezig"
Checked="True" Selected="True" thumb="" tekst="" />
<Node Text="IMG_2807 (Small).JPG"
Value="../../CMS/Images/Fotogalerie/552/IMG_2807 (Small).JPG" tekst="Afbeelding IMG_2807 (Small).JPG"
thumb="../../CMS/Images/Thumbs/552/IMG_2807 (Small).JPG" />
请注意,在第三行节点enabled=False
。
我使用的代码
XDocument doc = XDocument.Parse(strFile);
var values = (from f in doc.Elements().Descendants()
select f.Attribute("Value").Value).ToArray();
,这将引发一个错误..
答
你需要为null检查的价值,因为如果你不select f.Attribute("Value").Value
空检查时,它会如果抛出一个异常元素没有Value
属性。
看着你的示例XML不是所有的Nodes
都有属性Value
。
试试这个:
var values = (from f in doc.Descendants("Node")
where f.Attribute("Value") != null
select f.Attribute("Value").Value).ToArray();