public class XmlConfig
{
private XmlDocument doc;
private string xmlFileName;
private MemoryStream xmlStream;
private byte[] xmlcontent = new byte[0];
public byte[] Content
{
get
{
return System.Text.Encoding.Default.GetBytes(doc.InnerXml);
}
set
{
}
}
public XmlConfig(string filename)
{
xmlFileName = filename;
doc = new XmlDocument();
try
{
doc.Load(xmlFileName);
}
catch
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
}
} public XmlConfig(byte[] contain)
{
xmlStream = new MemoryStream(); xmlStream.Write(contain,0,contain.Length);
// MemoryStream stream =new MemoryStream(contain);
// xmlStream = (Stream) stream;
doc = new XmlDocument();
try
{
if(contain.Length==0)
doc.Load(xmlStream);
else
{
string xml = System.Text.Encoding.Default.GetString(contain);
doc.LoadXml(xml);
}
}
catch(Exception ee)
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
}
}
public void Save()
{
try
{
doc.Save(xmlFileName);
}
catch
{
}
}
public void StreamSave()
{
try
{
doc.Save(xmlStream);
}
catch(Exception ee)
{
Console.WriteLine(ee.ToString()); } } public string Read(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
return node.InnerText;
else
return value;
} public void Write(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
node.InnerText = value;
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}
node.InnerText = value;
}
}
//增加一个父节点
public void Write(string key)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}
}
}
public void ChildWrite(string key,string Childkey,string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
node2.InnerText=value;
}
else
{
XmlNode node3=doc.CreateElement(Childkey);
node.PrependChild(node3);
node3.InnerText=value;
}
}
//key为父节点的值,chlidkey为子节点的值。
public string Read(string key,string Childkey,string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if(node!=null)
{
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
return node2.InnerText;
}
else
return value;
}
else
return value;
}
public XmlNodeList NodeRead()
{
XmlNode node = doc.ChildNodes[1];
XmlNodeList nodelist = node.ChildNodes;
return nodelist;
}
public XmlNodeList NodeRead(string key)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
return node.ChildNodes;
}
public XmlNodeList NodeList()
{
return doc.ChildNodes;
}
}
{
private XmlDocument doc;
private string xmlFileName;
private MemoryStream xmlStream;
private byte[] xmlcontent = new byte[0];
public byte[] Content
{
get
{
return System.Text.Encoding.Default.GetBytes(doc.InnerXml);
}
set
{
}
}
public XmlConfig(string filename)
{
xmlFileName = filename;
doc = new XmlDocument();
try
{
doc.Load(xmlFileName);
}
catch
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
}
} public XmlConfig(byte[] contain)
{
xmlStream = new MemoryStream(); xmlStream.Write(contain,0,contain.Length);
// MemoryStream stream =new MemoryStream(contain);
// xmlStream = (Stream) stream;
doc = new XmlDocument();
try
{
if(contain.Length==0)
doc.Load(xmlStream);
else
{
string xml = System.Text.Encoding.Default.GetString(contain);
doc.LoadXml(xml);
}
}
catch(Exception ee)
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
}
}
public void Save()
{
try
{
doc.Save(xmlFileName);
}
catch
{
}
}
public void StreamSave()
{
try
{
doc.Save(xmlStream);
}
catch(Exception ee)
{
Console.WriteLine(ee.ToString()); } } public string Read(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
return node.InnerText;
else
return value;
} public void Write(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
node.InnerText = value;
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}
node.InnerText = value;
}
}
//增加一个父节点
public void Write(string key)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}
}
}
public void ChildWrite(string key,string Childkey,string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
node2.InnerText=value;
}
else
{
XmlNode node3=doc.CreateElement(Childkey);
node.PrependChild(node3);
node3.InnerText=value;
}
}
//key为父节点的值,chlidkey为子节点的值。
public string Read(string key,string Childkey,string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if(node!=null)
{
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
return node2.InnerText;
}
else
return value;
}
else
return value;
}
public XmlNodeList NodeRead()
{
XmlNode node = doc.ChildNodes[1];
XmlNodeList nodelist = node.ChildNodes;
return nodelist;
}
public XmlNodeList NodeRead(string key)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
return node.ChildNodes;
}
public XmlNodeList NodeList()
{
return doc.ChildNodes;
}
}
补充一下,操作是针对数据库的clob字段的,它是以xml形式保存数据的
using System.Xml;
using System.IO;
private XmlDocument doc;
//插入节点
doc.Load("books.xml");
XmlElement newBook=doc.CreateElement("book");
newBook.SetAttribute("genre","Mystery");
newBook.SetAttribute("publicationdate","2001");
newBook.SetAttribute("ISBN","123456789");
//
XmlElement newTitle=doc.CreateElement("title");
newTitle.InnerText="Case of the Missing Cookie";
newBook.AppendChild(newTitle);
//
XmlElement newAuthor=doc.CreateElement("author");
newBook.AppendChild(newAuthor);
//
XmlElement newName=doc.CreateElement("name");
newName.InnerText="C.Monster";
newAuthor.AppendChild(newName);
//
XmlElement newPrice=doc.CreateElement("price");
newPrice.InnerText="9.95";
newBook.AppendChild(newPrice);
//
doc.DocumentElement.AppendChild(newBook);
//
XmlTextWriter tr=new XmlTextWriter("booksedit.xml",null);
tr.Formatting=Formatting.Indented;
doc.WriteContentTo(tr);
tr.Close();
XmlNodeList nodeLst=doc.GetElementsByTagName("title");
foreach(XmlNode node in nodeLst)
listBox1.Items.Add(node.InnerText
string Nowmy=System.DateTime.Now.ToString();
doc.Load("history.xml");
int total=listViewReco.Items.Count;
for(int i=0;i<total;i++) {
XmlElement newRecord=doc.CreateElement("record");
newRecord.SetAttribute("name",listViewReco.Items[i].SubItems[0].Text.Trim());
newRecord.SetAttribute("mobile",listViewReco.Items[i].SubItems[1].Text.Trim());
newRecord.SetAttribute("content",listViewReco.Items[i].SubItems[2].Text.Trim());
newRecord.SetAttribute("SendDate",Nowmy);
doc.DocumentElement.AppendChild(newRecord);
}
XmlTextWriter tr=new XmlTextWriter("history.xml",null);
tr.Formatting=Formatting.Indented;
doc.WriteContentTo(tr);
tr.Close();