XmlDocument.LoadXml和Load的區別


 

LoadXml:從指定的字符串加載 XML 文檔。

eg:doc.LoadXml("<root>aa</root>");

       public void LoadXmlTest() {
            // Create the XmlDocument.
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<item><name>wrench</name></item>");

            // Add a price element.
            XmlElement newElem = doc.CreateElement("price");
            newElem.InnerText = "10.95";
            doc.DocumentElement.AppendChild(newElem);

            XmlNode xmlNode = doc.SelectSingleNode("/item/name");
            Console.WriteLine(xmlNode.InnerText);
            xmlNode = doc.SelectSingleNode("/item/price");
            Console.WriteLine(xmlNode.InnerText);

            // Save the document to a file and auto-indent the output.
            XmlTextWriter writer = new XmlTextWriter("data.xml", null);
            writer.Formatting = Formatting.Indented;
            doc.Save(writer);
        }

Load:加載指定的 XML 數據

XmlDocument.Load (Stream)從指定的流加載 XML 文檔。
XmlDocument.Load (String) 從指定的 URL 加載 XML 文檔。
XmlDocument.Load (TextReader) 從指定的 TextReader 加載 XML 文檔。
XmlDocument.Load (XmlReader)從指定的 XmlReader 加載 XML 文檔。

        public void getInfo(string fileName)
        {
            //創建XML的根節點
           // CreateXMLElement();
            string fileFullPath = Application.StartupPath + "\\" + fileName;
            Console.WriteLine(fileFullPath);
            XmlDocument doc = new XmlDocument();
            doc.Load(fileFullPath);


            XmlNodeList xmlNodeList = doc.SelectNodes("/root/business/item");
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                Console.WriteLine(string.Format("{0}\t{1} \n{2}", xmlNode.Attributes["BusinessName"].Value, xmlNode.Attributes["DistinctionKey"].Value, xmlNode.Attributes["Url"].Value));
            }

            Console.ReadLine();
        }
 
http://msdn.microsoft.com/zh-cn/library/system.xml.xmldocument.loadxml(VS.80).aspx
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM