这是我试图获取标签元素 Posted_Status 的 XML 文件,其中 Posted_Status 已就绪
<?xml version="1.0" encoding="utf-8"?>
<Server>
<Network> <---Network is the parent element
<Posted_Status id="10">Ready</Posted_Status>
<Timed_On id="10">7/28/2013 9:32:10 AM</Timed_On>
<Timed_Off id="10">8/28/2013 9:32:10 AM</Timed_Off>
</Network>
</Server>
我遇到 linq 查询返回 null 的问题。我正在尝试查询 XML 元素。元素名称是Posted_Status
。标记值为“就绪”。我正在尝试获取Posted_Status
Posted_Status 等于“Ready”的标签。
// Query's the tag where the tag equals Ready
IEnumerable<XElement> expiration =
from exp in main.Elements("Posted_Status")
where (string)exp.Element("Posted_Status").Value == "Ready"
select exp;
这会执行或调用查询,并显示Posted_Status
XML 标记中标记值等于“Ready”的所有值。
foreach (string exp in expiration)
{
for (int i = 0; i < IntializedPostStat.Count(); i++)
{
IntializedPostStat[i] = exp.ToString();
lstIntializations.Items.Add("[Posted_Status]......"
+ IntializedPostStat[i].ToString());
break;
}
}