laomin1985 2023-06-11 00:28 采纳率: 33.3%
浏览 14

xml文件,C#如何根据变量name,输出age。

xml文件,C#如何根据变量name,输出age。

例如name_age.xml

<?xml version="1.0" encoding="UTF-8"?>
<people>
<person name="白晶晶" age="28"></person>
<person name="至尊宝" age="300"></person>
</people>

  • 写回答

2条回答 默认 最新

  • IT技术分享社区 优质创作者: 数据库技术领域 2023-06-11 09:08
    关注

    可以参考如下代码读取Xml

    
    using System;
    using System.Xml;
    
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("path/to/your/xml/file.xml");
    
            XmlNodeList nodes = doc.SelectNodes("/root/element");
            foreach (XmlNode node in nodes)
            {
                string attrValue = node.Attributes["attributeName"].Value;
                string textValue = node.InnerText;
    
                Console.WriteLine("Attribute: {0}, Text: {1}", attrValue, textValue);
            }
        }
    }
    
    评论

报告相同问题?

问题事件

  • 修改了问题 6月11日
  • 修改了问题 6月11日
  • 创建了问题 6月11日