gpt4 book ai didi

c# - 为什么我的 XmlReader.GetAttribute() 没有返回值?

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

我正在尝试用 C# 解析我的 XML。

这是相关文件的一部分:

<holder name="wnd_login" width="300" x="20" height="180">...</holder>

这是应该读取它的代码:

while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "holder":
Holder holder = new Holder(reader.GetAttribute("name"));
...
}
}
}

我读到,常见的错误是忘记检查元素是否是起始元素。我添加了它,但 GetAttribute 仍然返回 null。有什么想法吗?

最佳答案

也许您需要先使用 XPath 表示法获取 XmlNode,然后像这样遍历 XmlNode:

foreach(XmlNode node in XmlNodes){     if (node["holder"].HasAttribues != null && node["holder"].Attributes.Count >1){        for (int i = 0; i < node["holder"].Attributes.Count; i++){             try{                XmlAttribute attr = node["holder"].Attributes[i];                if (attr != null){                     ....                }             }catch(XmlException xmlEx){                // Do something here with this...output to log?             }        }     }}

希望对您有所帮助,最好的祝福,汤姆。

关于c# - 为什么我的 XmlReader.GetAttribute() 没有返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2349543/

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