XML Read And write
public abstract class DocBase
{
protected String fileName = "";
protected XmlNode node = null;
public XmlDocument xmlDoc;
public int count;
public virtual void read()
{
}
public virtual void add()
{
}
protected void save(XmlDocument doc)
{
XmlTextWriter writer = new XmlTextWriter(fileName, null);
writer.Formatting = Formatting.Indented;
doc.WriteTo(writer);
writer.Flush();
writer.Close();
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Lib
{
public class DocScreen : DocBase
{
public DocScreen(String File)
{
fileName = File;
xmlDoc = new XmlDocument();
xmlDoc.Load(File);
count = 0;
}

public Screens[] readAll()
{
Screens[] all = new Screens[10000];
XmlNode xn = xmlDoc.SelectSingleNode("Config");//查找
XmlNodeList xnl = xn.ChildNodes;
//XmlElement xe1=xmlDoc.GetElementsByTagName("CameraController");
foreach (XmlNode xnf in xnl)
{
// XmlElement xe = (XmlElement)xnf;
if (xnf.Name == "Screens")
{
XmlNodeList xnl2 = xnf.ChildNodes;
foreach (XmlNode xnf2 in xnl2)
{
XmlElement xe = (XmlElement)xnf2;
all[count].PatrolIP = xe.GetAttribute("PatrolIP").Split(',');//显示属性值
all[count].PatroTime = xe.GetAttribute("PatroTime");
all[count].ScreenIndex = xe.GetAttribute("ScreenIndex");
all[count].ScreenMode = xe.GetAttribute("ScreenMode");
all[count].Title = xe.GetAttribute("Title");
//all[count].NetGate = "";
//all[count].NetMask = "";
//all[count].Runtime = "";
//all[count].VideoType = "";
count = count + 1;
}
break;
}
}
return all;
}

public Screens read(String ScreenMode, String ScreenIndex)
{
Screens[] all;
Screens r = new Screens();
all = this.readAll();
for (int i = 0; i < count; i++)
{
if (all[i].ScreenMode == ScreenMode && all[i].ScreenIndex == ScreenIndex)
{
r = all[i];
break;
}
}
return r;
}

public bool add(ScreenDetailInfo sreenDetailInfo)
{
try
{
node = xmlDoc.SelectSingleNode("//Config");
if (node != null)
{
XmlElement addElem = (XmlElement)node.SelectSingleNode("//Screens");
if (addElem == null)
{
XmlElement ScreenCofig = xmlDoc.CreateElement("Screens");
node.AppendChild(ScreenCofig);
}
node = xmlDoc.SelectSingleNode("//Screens");
XmlElement temp = (XmlElement)xmlDoc.SelectSingleNode("//Screen[@ScreenMode='" + sreenDetailInfo.ScreenMode + "'][@ScreenIndex='" + sreenDetailInfo.ScreenIndex + "']");
if (temp != null)
{
node.RemoveChild(temp);
}
XmlElement first = xmlDoc.CreateElement("Screen");
first.SetAttribute("ScreenMode", sreenDetailInfo.ScreenMode);
first.SetAttribute("ScreenIndex", sreenDetailInfo.ScreenIndex);
first.SetAttribute("PatroTime", sreenDetailInfo.PatroTime);
first.SetAttribute("Title", sreenDetailInfo.Title);
first.SetAttribute("PatrolIP", sreenDetailInfo.PatrolIP);
node.AppendChild(first);
save(xmlDoc);
}
return true;
}
catch (Exception ex)
{
Console.WriteLine("Write Screen Detail Info Error: " + ex.ToString());
return false;
}
}
}
}

浙公网安备 33010602011771号