今天开发一个WPF模块需要本地化保存一些用户设置,鉴于数据量不大,用XML。 (要是再小的话可以用Resources 和 Settings)。

清晰简短教程移步:http://bdk82924.iteye.com/blog/564353

代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Kinect; namespace KinectHost
{
class SettingsXMLConfig
{
public SettingsXMLConfig(){ createXML();
} public void AddDoor(string name, string x, string y, string z, string threshold)
{
try
{
XmlElement doors = null;
XmlElement theDoor = null, theName = null, theX = null, theY = null, theZ = null, theThreshold = null, root = null; string path = GetFolderPath();
string filepath = Path.Combine(path, "XMLSettings.xml");
if (File.Exists(filepath))
{
XmlDocument xmlSetting = new XmlDocument();
xmlSetting.Load(filepath); root = xmlSetting.DocumentElement;
doors = (XmlElement)root.SelectSingleNode("/XMLSettings/doors"); theDoor = xmlSetting.CreateElement("door");
theName = xmlSetting.CreateElement("name");
theName.InnerText = name;
theX = xmlSetting.CreateElement("x");
theX.InnerText = x;
theY = xmlSetting.CreateElement("y");
theY.InnerText = y;
theZ = xmlSetting.CreateElement("z");
theZ.InnerText = z;
theThreshold = xmlSetting.CreateElement("threshold");
theThreshold.InnerText = threshold; theDoor.AppendChild(theName);
theDoor.AppendChild(theX);
theDoor.AppendChild(theY);
theDoor.AppendChild(theZ);
theDoor.AppendChild(theThreshold); doors.AppendChild(theDoor); xmlSetting.Save(filepath);
}
else
{
MessageBox.Show("XML Settings file doesn't extist!");
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
} public void RemoveDoor(string name)
{
XmlElement root = null, door = null;
try
{
string path = GetFolderPath();
string filepath = Path.Combine(path, "XMLSettings.xml");
if (File.Exists(filepath))
{
XmlDocument xmlSetting = new XmlDocument();
xmlSetting.Load(filepath); root = xmlSetting.DocumentElement;
door = (XmlElement)root.SelectSingleNode("/XMLSettings/doors/door[name='" + name + "']");
door.ParentNode.RemoveChild(door); xmlSetting.Save(filepath);
}
else
{
MessageBox.Show("XML Settings file doesn't extist!");
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
} public List<CrossDoorDetector> SelectDoors(KinectSensor kinectSensor){
XmlElement root = null, doors = null;
List<CrossDoorDetector> crossDoorDetectorList = new List<CrossDoorDetector>();
try
{
string path = GetFolderPath();
string filepath = Path.Combine(path, "XMLSettings.xml");
if (File.Exists(filepath))
{
XmlDocument xmlSetting = new XmlDocument();
xmlSetting.Load(filepath); root = xmlSetting.DocumentElement;
doors = (XmlElement)root.SelectSingleNode("/XMLSettings/doors"); string name = "";
float x = ;
float y = ;
float z = ;
double threshold = ;
if (doors.ChildNodes.Count > )
{
//XmlNode xn = doors.SelectSingleNode("door");
XmlNodeList xnl = doors.ChildNodes;
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
name = ((XmlElement)xe.SelectSingleNode("name")).InnerText;
float.TryParse(((XmlElement)xe.SelectSingleNode("x")).InnerText, out x);
float.TryParse(((XmlElement)xe.SelectSingleNode("y")).InnerText, out y);
float.TryParse(((XmlElement)xe.SelectSingleNode("z")).InnerText, out z);
double.TryParse(((XmlElement)xe.SelectSingleNode("threshold")).InnerText, out threshold);
crossDoorDetectorList.Add(new CrossDoorDetector(name, x, y, z, threshold, kinectSensor));
}
return crossDoorDetectorList;
}
else
{
return null;
} }
else
{
MessageBox.Show("XML Settings file doesn't extist!");
return null;
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
return null;
}
} public void AddObject(string name, string x, string y, string z, string radius)
{
try
{
XmlElement doors = null;
XmlElement theObject = null, theName = null, theX = null, theY = null, theZ = null, ObjectRadius = null, root = null; string path = GetFolderPath();
string filepath = Path.Combine(path, "XMLSettings.xml");
if (File.Exists(filepath))
{
XmlDocument xmlSetting = new XmlDocument();
xmlSetting.Load(filepath); root = xmlSetting.DocumentElement;
doors = (XmlElement)root.SelectSingleNode("/XMLSettings/objects"); theObject = xmlSetting.CreateElement("object");
theName = xmlSetting.CreateElement("name");
theName.InnerText = name;
theX = xmlSetting.CreateElement("x");
theX.InnerText = x;
theY = xmlSetting.CreateElement("y");
theY.InnerText = y;
theZ = xmlSetting.CreateElement("z");
theZ.InnerText = z;
ObjectRadius = xmlSetting.CreateElement("radius");
ObjectRadius.InnerText = radius; theObject.AppendChild(theName);
theObject.AppendChild(theX);
theObject.AppendChild(theY);
theObject.AppendChild(theZ);
theObject.AppendChild(ObjectRadius); doors.AppendChild(theObject); xmlSetting.Save(filepath);
}
else
{
MessageBox.Show("XML Settings file doesn't extist!");
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
public List<ObjectPointing> SelectObjects()
{
XmlElement root = null, objects = null;
List<ObjectPointing> objectPointingList = new List<ObjectPointing>();
try
{
string path = GetFolderPath();
string filepath = Path.Combine(path, "XMLSettings.xml");
if (File.Exists(filepath))
{
XmlDocument xmlSetting = new XmlDocument();
xmlSetting.Load(filepath); root = xmlSetting.DocumentElement;
objects = (XmlElement)root.SelectSingleNode("/XMLSettings/objects"); string name = "";
float x = ;
float y = ;
float z = ;
float radius = ;
if (objects.ChildNodes.Count > )
{
//XmlNode xn = doors.SelectSingleNode("door");
XmlNodeList xnl = objects.ChildNodes;
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
name = ((XmlElement)xe.SelectSingleNode("name")).InnerText;
float.TryParse(((XmlElement)xe.SelectSingleNode("x")).InnerText, out x);
float.TryParse(((XmlElement)xe.SelectSingleNode("y")).InnerText, out y);
float.TryParse(((XmlElement)xe.SelectSingleNode("z")).InnerText, out z);
float.TryParse(((XmlElement)xe.SelectSingleNode("radius")).InnerText, out radius);
objectPointingList.Add(new ObjectPointing(name, x, y, z, radius));
}
return objectPointingList;
}
else
{
return null;
} }
else
{
MessageBox.Show("XML Settings file doesn't extist!");
return null;
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
return null;
}
}
public void RemoveObject(string name)
{
XmlElement root = null, Object = null;
try
{
string path = GetFolderPath();
string filepath = Path.Combine(path, "XMLSettings.xml");
if (File.Exists(filepath))
{
XmlDocument xmlSetting = new XmlDocument();
xmlSetting.Load(filepath); root = xmlSetting.DocumentElement;
Object = (XmlElement)root.SelectSingleNode("/XMLSettings/objects/object[name='" + name + "']");
Object.ParentNode.RemoveChild(Object); xmlSetting.Save(filepath);
}
else
{
MessageBox.Show("XML Settings file doesn't extist!");
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
} public void AddLocation(string name, string x, string y, string z)
{
try
{
XmlElement doors = null;
XmlElement theLocation = null, theName = null, theX = null, theY = null, theZ = null, root = null; string path = GetFolderPath();
string filepath = Path.Combine(path, "XMLSettings.xml");
if (File.Exists(filepath))
{
XmlDocument xmlSetting = new XmlDocument();
xmlSetting.Load(filepath); root = xmlSetting.DocumentElement;
doors = (XmlElement)root.SelectSingleNode("/XMLSettings/locations"); theLocation = xmlSetting.CreateElement("location");
theName = xmlSetting.CreateElement("name");
theName.InnerText = name;
theX = xmlSetting.CreateElement("x");
theX.InnerText = x;
theY = xmlSetting.CreateElement("y");
theY.InnerText = y;
theZ = xmlSetting.CreateElement("z");
theZ.InnerText = z; theLocation.AppendChild(theName);
theLocation.AppendChild(theX);
theLocation.AppendChild(theY);
theLocation.AppendChild(theZ); doors.AppendChild(theLocation); xmlSetting.Save(filepath);
}
else
{
MessageBox.Show("XML Settings file doesn't extist!");
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
public List<LocationDetector> SelectLocations()
{
XmlElement root = null, objects = null;
List<LocationDetector> locationDetectorList = new List<LocationDetector>();
try
{
string path = GetFolderPath();
string filepath = Path.Combine(path, "XMLSettings.xml");
if (File.Exists(filepath))
{
XmlDocument xmlSetting = new XmlDocument();
xmlSetting.Load(filepath); root = xmlSetting.DocumentElement;
objects = (XmlElement)root.SelectSingleNode("/XMLSettings/locations"); string name = "";
float x = ;
float y = ;
float z = ;
if (objects.ChildNodes.Count > )
{
//XmlNode xn = doors.SelectSingleNode("door");
XmlNodeList xnl = objects.ChildNodes;
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
name = ((XmlElement)xe.SelectSingleNode("name")).InnerText;
float.TryParse(((XmlElement)xe.SelectSingleNode("x")).InnerText, out x);
float.TryParse(((XmlElement)xe.SelectSingleNode("y")).InnerText, out y);
float.TryParse(((XmlElement)xe.SelectSingleNode("z")).InnerText, out z);
locationDetectorList.Add(new LocationDetector(name, x, y, z));
}
return locationDetectorList;
}
else
{
return null;
} }
else
{
MessageBox.Show("XML Settings file doesn't extist!");
return null;
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
return null;
}
}
public void RemoveLocation(string name)
{
XmlElement root = null, location = null;
try
{
string path = GetFolderPath();
string filepath = Path.Combine(path, "XMLSettings.xml");
if (File.Exists(filepath))
{
XmlDocument xmlSetting = new XmlDocument();
xmlSetting.Load(filepath); root = xmlSetting.DocumentElement;
location = (XmlElement)root.SelectSingleNode("/XMLSettings/locations/location[name='" + name + "']");
location.ParentNode.RemoveChild(location); xmlSetting.Save(filepath);
}
else
{
MessageBox.Show("XML Settings file doesn't extist!");
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
} private string GetFolderPath()
{
string folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XML");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
return folder;
} private void createXML(){
string path = GetFolderPath();
string filepath = Path.Combine(path, "XMLSettings.xml");
if (!File.Exists(filepath))
{
XmlDocument xmlSetting = new XmlDocument();
XmlElement root = xmlSetting.CreateElement("XMLSettings");
xmlSetting.AppendChild(root); XmlElement doors = xmlSetting.CreateElement("doors");
XmlElement objects = xmlSetting.CreateElement("objects");
XmlElement locations = xmlSetting.CreateElement("locations");
root.AppendChild(doors);
root.AppendChild(objects);
root.AppendChild(locations);
//string folder = Path.Combine(path, "XMLSettings.xml");
xmlSetting.Save(filepath);
}
} }
}

XML结果:

<XMLSettings>
<doors>
<door>
<name>Bedroom0</name>
<x>165.5378</x>
<y>208.586</y>
<z>2.553757</z>
<threshold>66.0010801284721</threshold>
</door>
</doors>
<objects>
<object>
<name>Light</name>
<x>-0.1636572</x>
<y>0.762675</y>
<z>1.719781</z>
<radius>0.3</radius>
</object>
</objects>
<locations>
<location>
<name>Desk</name>
<x>0.02907251</x>
<y>0.02481232</y>
<z>2.383653</z>
</location>
</locations>
</XMLSettings>

  

C# XMLDocument的更多相关文章

  1. 用ORM的思想操作XML文档,一个对象就搞定不要太简单。滚蛋吧!XmlDocument、XmlNode、Xml***……

    大家有没有这样的感受,一涉及XML文档操作就得百度一遍.是不是非!常!烦!.各种类型,各种方法,更别提为了找到一个节点多费劲.本来想写个XML操作的工具方法,写了两行一想既然XML文档是有规律的,如果 ...

  2. XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate

    namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...

  3. C# XML技术总结之XDocument 和XmlDocument

    引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...

  4. c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)

    主界面

  5. jquery中ajax在firefox浏览器下“object XMLDocument”返回结果的解决办法

    asp.net中借助jquery的ajax处理功能,使用起来很方便.但是在firefox下获得的data报错object XMLDocument.这是因为默认的情况下把datatype用html来解析 ...

  6. XmlDocument解析Soap格式文件案例:

    private static string Analysis(string strResult) { var doc = new System.Xml.XmlDocument(); //加载soap文 ...

  7. 将XmlDocument转换成XDocument

    XmlDocument xml=new XmlDocument(); xml.LoadXml(strXmlText); XmlReader xr=new XmlNodeReader(xml); XDo ...

  8. XmlDocument To String

    一.从String xml到XmlDocument的: string xml = "<XML><Test>Hello World</Test></X ...

  9. c# xml的增删改查操作 xmlDocument 的用法

    1.将xml转换为DataTable string path = "";//xml的位置StringReader sr = null;XmlTextReader xmlReader ...

随机推荐

  1. LVM 管理减少swap分区空间增加到根分区

    简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现 ...

  2. android存储方式的应用场景

    作为一个完整的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别是:SharePreference.文件存储.SQLite. Content Provider ...

  3. python学习1

    1.由于win8的电脑上出现了0xc0000b错误不能解决,所以现在使用的是虚拟机中的Linux系统.安装过程见http://blog.csdn.net/yuzhongchun/article/det ...

  4. 怎么定制属于自己的GitHub主页呢?

    Either you fuck the life or the life fucks you. My personal GitHub page Creating a GitHub Pages site ...

  5. [No0000AB]用Visual Studio 2015在 WIN10 64bit 上编译7-zip (32 bit)

    1.7-ZIP简介 7-zip 是一款免费的压缩解压软件.ZIP格式的文件默认被苹果和微软支持,完全不需要额外安装其他软件就可以解压.但对于非US-ASCII编码的文件名和大于2GB的ZIP文件,可能 ...

  6. Linux之shell篇

    shell是用户与系统交互的界面,这是基本方式之一.标准的shell为bash. shell的操作: 显示所有使用过的命令:history. 执行最近执行过的一条指令:!!.首先会给出执行的是哪一条指 ...

  7. .net(C#)中this关键字

    使用this关键字引用成员变量使用this关键字在自身构造方法内部引用其它构造方法使用this关键字代表自身类的对象使用this关键字引用成员方法 在一个类的方法或构造方法内部,可以使用"t ...

  8. CDQ分治

    要求可以计算前面的操作对后面询问的贡献 BZOJ1176 #include <cstdio> #include <algorithm> using namespace std; ...

  9. odoo种种

    [精]Odoo 8.0深入浅出开发教程-模块开发基础 http://blog.csdn.net/sunansheng/article/details/50864527 搭建odoo开发调试环境 htt ...

  10. IIS配置MP3/MP4/OGG/flv等资源文件访问

    配置过程参考:http://www.cnblogs.com/EasonJim/p/4752399.html 以下包含了mp4的mime类型: 323 text/h323 acx application ...