怎么读出Xml文件中某个节点、属性的信息

待处理的Xml文件:

<?xml version="1.0" encoding='UTF-8'?>
<ufinterface billtype="gl" filename="gledi" isexchange="Y" proc="add" receiver="01" replace="Y" roottag="sendresult" sender="ceec" successful="Y">
	<sendresult>
		<billpk>
		</billpk>
		<bdocid>AAAA</bdocid>
		<filename>BBBB</filename>
		<resultcode>CCCC</resultcode>
		<resultdescription>DDDD</resultdescription>
		<content>EEEE</content>
	</sendresult>
	<sendresult>
		<billpk>
		</billpk>
		<bdocid>aaaa</bdocid>
		<filename>bbbb</filename>
		<resultcode>cccc</resultcode>
		<resultdescription>dddd</resultdescription>
		<content>eeee</content>
	</sendresult>
</ufinterface>

处理目标获取节点isexchange、billpk、resultcode、bdocid、resultdescription的节点信息,代码如下:
private void ReadXml()
        {
            try
            {
                XmlDocument Xd = new XmlDocument();
                //Xd.LoadXml(strXml);
                Xd.Load("E:\\Books.xml");//加载待处理的xml文件
                XmlNode root = Xd.DocumentElement;
                string strID = string.Empty;
                string Xtfhz = string.Empty;//<billpk>
                string Fhxxbh = string.Empty;//<resultcode>
                string Pzbh = string.Empty;//<bdocid>
                string Djxx = string.Empty;//<resultdescription>
                //遍历每个ufinterface节点
                foreach (XmlNode node in Xd.SelectNodes("//ufinterface"))
                {
                    strID = node.Attributes["isexchange"].Value; //获取isexchange属性的值
                    XmlNode RootNode = Xd.SelectSingleNode("ufinterface");//得到根节点
                    //得到根节点下所有名为“sendresult”子节点,是一个list集合。
                    XmlNodeList ChildNodes = RootNode.SelectNodes("sendresult");
                    //遍历<sendresult>节点集合
                    foreach (XmlNode childnode in ChildNodes)
                    {
                        //遍历每个<sendresult>节点中的子节点
                        foreach (XmlNode snode in childnode)
                        {
                            if (snode.Name == "billpk")
                            {
                                Xtfhz = snode.InnerText;
				MessageBox.Show("billpk=" + Xtfhz);
                            }
                            if (snode.Name == "bdocid")
                            {
                                Pzbh = snode.InnerText;
				 MessageBox.Show("bdocid=" + Pzbh);
                            }
                            if (snode.Name == "resultcode")
                            {
                                Fhxxbh = snode.InnerText;
				MessageBox.Show("resultcode=" + Fhxxbh);
                            }
                            if (snode.Name == "resultdescription")
                            {
                                Djxx = snode.InnerText;
				MessageBox.Show("resultdescription=" + Djxx);
                            }
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }
在代码中的每个MessageBox.Show均可弹出,对应节点的信息。

上一篇:【原生JS】自动渐变轮播


下一篇:原生js写一个无缝轮播图插件(支持vue)