閱讀359 返回首頁    go 阿裏雲 go 技術社區[雲棲]


怎麼讀出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("bdocresultcode")
                            {
                                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均可彈出,對應節點的信息。

最後更新:2017-04-03 12:54:19

  上一篇:go 正則表達式基礎語法
  下一篇:go 隱藏GridControl的“Drag a column header here to group by that column”