用net做一个程序来解析xml
使用.NET可以使用XmlDocument类来解析XML。以下是一个简单的示例代码:
using System;
using System.Xml;
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("path/to/xml/file.xml");
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("elementName");
foreach (XmlNode node in nodeList)
{
// 解析XML节点的数据
string attributeValue = node.Attributes["attributeName"].Value;
string elementValue = node.InnerText;
Console.WriteLine("Attribute: " + attributeValue);
Console.WriteLine("Element: " + elementValue);
}
}
}
请确保将path/to/xml/file.xml替换为实际的XML文件路径,并将elementName和attributeName替换为实际的元素和属性名称。
此代码片段加载XML文件,然后使用GetElementsByTagName方法选择特定元素的节点列表。然后,使用节点的Attributes属性获取属性值,使用InnerText属性获取元素值。最后,使用Console.WriteLine将解析的数据打印到控制台上
原文地址: https://www.cveoy.top/t/topic/hysq 著作权归作者所有。请勿转载和采集!