//以下两种方法将获得的数据另存成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(多行相同的)的存取:
- <events>
- <event>
- <date>sample date 1</date>
- <name>sample name 1</name>
- </event>
- <event>
- <date>sample date 2</date>
- <name>sample name 2</name>
- </event>
- XmlDocument doc = new XmlDocument();
- doc.Load(URLString);
- XmlElement root = doc.DocumentElement;
- XmlNodeList nodes = root.SelectNodes("//events");
- foreach (XmlNode node in nodes)
- {
- string date = node["date"].InnerText;
- string name = node["name"].InnerText;
- MessageBox.Show("Date:"+date+" Name:"+name);
- 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;