gpt4 book ai didi

c# - 如何从 XML 返回 bool 值中获取值?

转载 作者:数据小太阳 更新时间:2023-10-29 03:01:36 25 4
gpt4 key购买 nike

我想做一个函数来输入值(funName)并检查 XML 文件属性(FunName)然后输出 XML 文件属性(isEnable)boolean true 或 false

如何修改这段代码?

我的 XML 文件

<itema>
<itemb FunName="ABC" isEnable="true"></itemb>
<itemb FunName="DEF" isEnable="false"></itemb>
</itema>

我的代码

public bool FunEnable(string funName , string isEnable)
{
bool result = true;
XmlDocument xDL = new XmlDocument();
xDL.Load("C://XMLFile2.xml"); //Load XML file

XmlNode xSingleNode = xDL.SelectSingleNode("//itemb");
XmlAttributeCollection xAT = xSingleNode.Attributes; //read all Node attribute
for (int i = 0; i < xAT.Count; i++)
{
if (xAT.Item(i).Name == "isEnable")
{
Console.WriteLine(xAT.Item(i).Value); //read we want attribute content
}
}
return result;
}

非常感谢

最佳答案

你可以试试这个:

public static bool FunEnable(string funNam)
{
bool result = true;
XmlDocument xDL = new XmlDocument();
xDL.Load(@"C:/XMLFile2.xml"); //Load XML file
XmlNodeList nodeList = xDL.SelectNodes("//itemb");
foreach (XmlNode node in nodeList)
{
if (node.Attributes["FunName"].Value.Equals(funNam))
{
result = Convert.ToBoolean(node.Attributes["isEnable"].Value);
break;
}
}
Console.WriteLine("with funName = "+ funNam +" isEnable equal to : " + result);
return result;
}

输出

with funName = ABC isEnable equal to : True

关于c# - 如何从 XML 返回 bool 值中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393591/

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