温馨提示×

c#如何获取xml节点中的属性值

c#
小亿
464
2024-07-27 02:13:12
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

您可以使用C#中的XmlDocument类来获取XML节点中的属性值。下面是一个示例代码片段,演示如何获取XML节点中的属性值:

using System;
using System.Xml;

class Program
{
    static void Main()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("data.xml"); // 加载XML文件

        // 获取根节点
        XmlNode root = doc.DocumentElement;

        // 遍历所有节点
        foreach (XmlNode node in root.ChildNodes)
        {
            // 获取节点的属性值
            XmlAttribute attribute = node.Attributes["attributeName"];
            if (attribute != null)
            {
                Console.WriteLine("属性值:" + attribute.Value);
            }
        }
    }
}

在上面的示例中,我们首先加载XML文件,然后获取根节点。然后通过遍历所有子节点,我们可以使用Attributes属性来获取节点中的属性,然后获取属性的值。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:c#如何获取object的属性值

0