internal class ReadXML
{
/// <summary>
/// 获取XML文件person节点信息
/// </summary>
/// <param name="filePath">xml文件路径</param>
/// <returns></returns>
internal object Get(string filePath)
{
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
StringBuilder sb = new StringBuilder();
foreach (XmlNode element in doc.DocumentElement.getElementsByTagName_r("person1"))
{
//1 获取person节点->name节点->address属性值
sb.Append(element["name"].GetAttribute("address"));
//2 获取person节点->name值
//sb.Append(element["name"].InnerText);
//3 获取person节点->name/sex/age属性值
//sb.Append(element["name"].InnerText)
//.Append(element["sex"].InnerText)
//.Append(element["age"].InnerText);
}
return sb;
}
}
static void Main(string[] args)
{
ReadXML read = new ReadXML();
Console.WriteLine(read.Get("person.xml").ToString());
Console.ReadKey();
{
/// <summary>
/// 获取XML文件person节点信息
/// </summary>
/// <param name="filePath">xml文件路径</param>
/// <returns></returns>
internal object Get(string filePath)
{
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
StringBuilder sb = new StringBuilder();
foreach (XmlNode element in doc.DocumentElement.getElementsByTagName_r("person1"))
{
//1 获取person节点->name节点->address属性值
sb.Append(element["name"].GetAttribute("address"));
//2 获取person节点->name值
//sb.Append(element["name"].InnerText);
//3 获取person节点->name/sex/age属性值
//sb.Append(element["name"].InnerText)
//.Append(element["sex"].InnerText)
//.Append(element["age"].InnerText);
}
return sb;
}
}
static void Main(string[] args)
{
ReadXML read = new ReadXML();
Console.WriteLine(read.Get("person.xml").ToString());
Console.ReadKey();
}
person.xml文件内容
<?xml version="1.0" encoding="utf-8" ?>
<ss>
<person name="张三" sex="男" age="21">
</person>
<person name="张三2" sex="男" age="22">
</person>
<person name="张三3" sex="男" age="23">
</person>
<person1>
<name address="四川成都">李四</name>
<sex>女</sex>
<age>23</age>
</person1>
简单的一句:select * from sysobjects for xml path('root') 以xml格式显示数据 path里面的参数是跟节点名称
</ss>