gpt4 book ai didi

c# - 检查 Xml 节点是否有属性

转载 作者:可可西里 更新时间:2023-11-01 08:55:22 25 4
gpt4 key购买 nike

如何检查节点是否具有特定属性。

我所做的是:

string referenceFileName = xmlFileName;
XmlTextReader textReader = new XmlTextReader(referenceFileName);

while (textReader.Read())
{
XmlNodeType nType = textReader.NodeType;

// if node type is an element
if (nType == XmlNodeType.Element)
{
if (textReader.Name.Equals("Control"))
{
if (textReader.AttributeCount >= 1)
{
String val = string.Empty;
val = textReader.GetAttribute("Visible");
if (!(val == null || val.Equals(string.Empty)))
{

}
}
}
}

是否有任何函数可以检查给定属性是否存在?

最佳答案

不,我认为 XmlTextReader 类中没有任何方法可以告诉您特定属性是否存在。

你可以做一件事来检查

if(null == textReader.GetAttribute("Visible"))
{
//this means attribute doesn't exist
}

因为MSDN关于GetAttribute 方法

    Return the value of the specified attribute. If the attribute is not found,
a null reference (Nothing in Visual Basic) is returned.

关于c# - 检查 Xml 节点是否有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8489051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com