.NET 中 XML 操作全解析
1. XML 节点属性操作
在处理 XML 时,节点属性操作是基础且重要的部分。例如, HasAttributes
属性可用于判断节点是否有属性,若有则返回 true
,否则返回 false
。 AttributeCount
属性能告知节点属性的数量,而 GetAttribute()
方法可通过名称或索引获取属性。若要逐个遍历属性,还可使用 MoveToFirstAttribute()
和 MoveToNextAttribute()
方法。
以下是一个遍历属性的示例代码:
protected void button1_Click (object sender, System.EventArgs e)
{
//set this path to match your data path structure
string fileName = "..\\..\\..\\books.xml";
//Create the new TextReader Object
XmlTextReader tr = new XmlTextReader(fileName);
//Read in node at a time
while(tr.Read())
{
//check to see if it’s a Node