pin*_*gve 3 c# xml linq linq-to-xml
说我有XElement对象doc:
<parameters mode="solve">
<inputs>
<a>value_a</a>
...
...
Run Code Online (Sandbox Code Playgroud)
我如何获得第一个元素(参数)的属性值,换句话说,我如何检查它是哪个模式.
如果我写
if ((string)doc.Element("parameters").Attribute("mode").Value == "solve") { mode = 1; }
Run Code Online (Sandbox Code Playgroud)
它给了我null对象引用错误
如果doc是XElement,如你在问题中所说,那么你可能不需要再次匹配它:
if (doc.Attribute("mode").Value.ToString() == "solve") {
mode = 1;
}
Run Code Online (Sandbox Code Playgroud)
如果是XDocument,则可以使用其Root属性来引用document元素:
if (doc.Root.Attribute("mode").Value.ToString() == "solve") {
mode = 1;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5583 次 |
| 最近记录: |