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>
可以参考如下代码读取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);
}
}
}
我搞定了