zoukankan      html  css  js  c++  java
  • 关于XML数据的处理【总结】

    //以下两种方法将获得的数据另存成XML

                  第一种: using (FileStream lv_YieldFs = new FileStream(saveFileDialog_result.FileName, FileMode.Append, FileAccess.Write))

                  {

                        using (StreamWriter lv_Yieldsw = new StreamWriter(lv_YieldFs))

                        {

                            lv_Yieldsw.Write(lv_msg );

                            lv_Yieldsw.Flush();

                        }}

                    第二种: XmlDocument lv_XmlDoc = new XmlDocument();

                    lv_XmlDoc.LoadXml(lv_msg);

                    lv_XmlDoc.PreserveWhitespace = true;

                    lv_XmlDoc .Save (saveFileDialog_result .FileName );

    XML的寫入:

    FileStream fs = new FileStream("products.xml", FileMode.Create);
            XmlWriter w = XmlWriter.Create(fs);
            w.WriteStartDocument();
            w.WriteStartElement("products");


            w.WriteStartElement ("product");
            w.WriteAttributeString("id", "1001");
            w.WriteElementString("productName", "Gourmet Coffee");
            w.WriteElementString("productPrice", "0.99");
            w.WriteEndElement();

            w.WriteStartElement("product");
            w.WriteAttributeString("id", "1002");
            w.WriteElementString("productName", "Tea Pot");
            w.WriteElementString("productPrice", "12.99");
            w.WriteEndElement();

            w.WriteEndElement();
            w.WriteEndDocument();
            w.Flush();
            fs.Close();

    xml(多行相同的)的存取:

    1. <events>
    2.   <event>
    3.     <date>sample date 1</date>
    4.     <name>sample name 1</name>
    5.   </event>
    6.   <event>
    7.     <date>sample date 2</date>
    8.     <name>sample name 2</name>
    9.   </event>
    10.  XmlDocument doc = new XmlDocument();
    11.             doc.Load(URLString);
    12.             XmlElement root = doc.DocumentElement;
    13.             XmlNodeList   nodes = root.SelectNodes("//events"); 
    14.  
    15.             foreach (XmlNode  node   in   nodes)
    16.             {
    17.                 string date = node["date"].InnerText;
    18.                 string name = node["name"].InnerText;
    19.                 MessageBox.Show("Date:"+date+" Name:"+name);
    20. 11.             }

    精确查找某个节点:

                    XmlDocument lv_XMLDoc = new XmlDocument();
                    XmlNode lv_XMLNode;
                    lv_XMLDoc.Load(lv_XMLPath[0]);                                             
                    lv_XMLNode = lv_XMLDoc.DocumentElement.SelectSingleNode("INFO/**");
                    string lv = lv_XMLNode.InnerText;  

  • 相关阅读:
    nodejs处理get请求
    ubuntu中chromium无法播放flash,安装flash
    JQuery日历控件
    深度理解Jquery 中 offset() 方法
    javascrip中parentNode和offsetParent之间的区别
    JQuery判断元素是否存在
    javascript日历控件——纯javascript版
    canvas作为背景
    background-position控制背景位置
    javascript日历控件
  • 原文地址:https://www.cnblogs.com/alexzp/p/2335506.html
Copyright © 2011-2022 走看看