本文介绍了如何检查XML中是否存在特定属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
部分XML内容:
<section name="Header">
<placeholder name="HeaderPane"></placeholder>
</section>
<section name="Middle" split="20">
<placeholder name="ContentLeft" ></placeholder>
<placeholder name="ContentMiddle"></placeholder>
<placeholder name="ContentRight"></placeholder>
</section>
<section name="Bottom">
<placeholder name="BottomPane"></placeholder>
</section>
我要检入每个节点,如果属性 split
存在,请尝试在变量中分配属性值.
I want to check in each node and if attribute split
exist, try to assign an attribute value in a variable.
在循环中,我尝试:
foreach (XmlNode xNode in nodeListName)
{
if(xNode.ParentNode.Attributes["split"].Value != "")
{
parentSplit = xNode.ParentNode.Attributes["split"].Value;
}
}
但是如果条件只检查值而不检查属性是否存在,那我是错的.我应该如何检查属性的存在?
But I'm wrong if the condition checks only the value, not the existence of attributes. How should I check for the existence of attributes?
推荐答案
您实际上可以直接索引到Attributes集合中(如果使用的是C#而不是VB):
You can actually index directly into the Attributes collection (if you are using C# not VB):
foreach (XmlNode xNode in nodeListName)
{
XmlNode parent = xNode.ParentNode;
if (parent.Attributes != null
&& parent.Attributes["split"] != null)
{
parentSplit = parent.Attributes["split"].Value;
}
}
这篇关于如何检查XML中是否存在特定属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!