Get ancestors list in an XML document

该博客展示了一个C#程序,使用System.Xml命名空间加载XML文档。程序创建了一个Windows窗体,包含一个按钮。点击按钮后,程序加载指定的XML文件,根据给定的URL和参数查找目标节点,并获取其祖先节点数组,最后显示这些节点的XML内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;

namespace ReadXmlDemo
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : Form
    
{
        
private Button button1;
        
private Container components = null;

        
public static void Main()
        
{
            Application.Run(
new Form1());
        }


        
public Form1()
        
{
            InitializeComponent();
        }


        
protected override void Dispose(bool disposing)
        
{
            
if (disposing) {
                
if (components != null{
                    components.Dispose();
                }

            }

            
base.Dispose(disposing);
        }


        
#region Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>

        private void InitializeComponent()
        
{
            
this.button1 = new Button();
            
this.SuspendLayout();
            
// 
            
// button1
            
// 
            this.button1.Location = new Point(17648);
            
this.button1.Name = "button1";
            
this.button1.TabIndex = 0;
            
this.button1.Text = "button1";
            
this.button1.Click += new EventHandler(this.button1_Click);
            
// 
            
// Form1
            
// 
            this.AutoScaleBaseSize = new Size(614);
            
this.ClientSize = new Size(292273);
            
this.Controls.Add(this.button1);
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.ResumeLayout(false);

        }


        
#endregion


        
private void button1_Click(object sender, EventArgs e)
        
{
            Setup();

            
string url = "/pmanager/scm/frmscmindex.aspx";
            
string param = "projectid";

            XmlNode[] ancestors 
= GetAncestorNodeList(url, param);

            
// test results
            if (ancestors != null{
                
foreach (XmlNode node in ancestors)
                    MessageBox.Show(node.OuterXml);
            }

        }


        
private XmlDocument doc;

        
/// <summary>
        
/// 加载 Xml 文档
        
/// </summary>

        private void Setup()
        
{
            doc 
= new XmlDocument();
            
try {
                doc.Load(Application.StartupPath 
+ "//nav.xml");
            }

            
catch {
                
throw new Exception("Xml 文件加载失败。");
            }

        }


        
/// <summary>
        
/// 得到指定节点的祖先节点的数组(强类型)
        
/// </summary>
        
/// <param name="url"></param>
        
/// <param name="param"></param>
        
/// <returns></returns>

        private XmlNode[] GetAncestorNodeList(string url, string param)
        
{
            XmlNode targetNode 
= doc.SelectSingleNode("//siteMapNode[@url='" + url + "' and @param='" + param + "']");
            
if (targetNode == null)
                
return null;

            ArrayList alAncestors 
= new ArrayList();

            
for (XmlNode node = targetNode.ParentNode;
                node 
!= null && node != doc.DocumentElement;
                node 
= node.ParentNode) {
                    alAncestors.Add(node);
            }

            alAncestors.Reverse();

            XmlNode[] nodeList 
= (XmlNode[]) alAncestors.ToArray(typeof (XmlNode));

            
return nodeList;
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值