XML -读取

1、SelectSingleNode()与SelectNode()区别

https://www.cnblogs.com/monian/p/3313243.html

SelectSingleNode:选择匹配 XPath 表达式的第一个 XmlNode

SelectNodes:选择匹配 XPath 表达式的结点集合 XmlNodeList

2、测试案例

            Hashtable ht = new Hashtable();
            string orgCodePath = "E:\\DefectDefinitions.xml";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(orgCodePath);
            //获取节点列表 
            XmlNodeList nodelist = xmldoc.SelectNodes("//defect");
            //XmlNode xn = nodelist.Item(2); 

            string strXmlKeyList = "Missing,NG,Scratch,Lift Lead,Bridge";
            string[] strXmlKeyArray = strXmlKeyList.Split(',',';');

            for(int i=0;i<strXmlKeyArray.Length;i++)
            {
                Console.WriteLine(strXmlKeyArray[i]);
            }
            string strXmlString = "";

            for (int i = 0; i < strXmlKeyArray.Length; i++)
            {
                foreach (XmlElement element in nodelist)
                {
                    if (strXmlKeyArray[i] == element.FirstChild.InnerText)
                    {
                        strXmlString = strXmlString + element["zh-Hans"].InnerText;
                    }

                    Console.WriteLine(strXmlString);
                }
            }
View Code

2、案例

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root>" +
                    "<elem>some text<child/>more text</elem>" +
                    "</root>");
    XmlNode xn= doc.SelectSingleNodes("//elem").Item(0);
    if (xn.HasChildNodes)
    {
        MessageBox.Show(xn.FirstChild.InnerText);   //some text
        MessageBox.Show(xn.LastChild.InnerText);    //more text
    }
View Code

 

posted @ 2019-06-27 18:07  kuaqi  阅读(190)  评论(0编辑  收藏  举报