XML在开发中作为文件存储格式、数据交换的协议用的非常普遍,各个编程语言有都支持。W3C也制定了XML DOM的标准。在这里主要介绍下.Net中的XmlDocument,包括xml读取和写入等功能。
一、Xml的加载读取
1、数据等准备
Xml测试数据:
<?xml version="1.0" encoding="UTF-8"?>
-<CameraGroup WKT="UNKNOWNCS["unnamed"]">
<Camera duration="5" comment="" roll="-4.29374881997575E-14" tilt="-15.333841267255" heading="-50.5252574662688" z="770.962000000316" y="24483.2877865981" x="10533.2696556843" Picture="b22d08c9-59f2-4b21-a254-d7133eb1b7bb.jpg" Name="初始界面"/>
<Camera duration="5" comment="" roll="-9.54166404439055E-15" tilt="-12.2364039278614" heading="-71.2583141496969" z="524.34103072128" y="24767.3735196134" x="10161.8880158652" Picture="6c7e6098-6064-401c-93c0-dce573f86b5d.jpg" Name="分区1"/>
</CameraGroup>
读取的数据,我们定义了一个实体类LocationCamera,用来保存Xml解析后的数据:
public
class
LocationCamera
{
public
LocationCamera()
{
}
private
string
name;
public
string
Name
{
get {
return
name; }
set { name =
value; }
}
private
int duration;
public
int Duration
{
get {
return
duration; }
set { duration =
value; }
}
private
double
roll;//="-4.29374881997575E-14"
public
double
Roll
{
get {
return
roll; }
set { roll =
value; }
}
private
double
tilt;//="-15.333841267255"
public
double
Tilt
{
get {
return
tilt; }
set { tilt =
value; }
}
private
double
heading;//="-50.5252574662688"
public
double
Heading
{
get {
return
heading; }
set { heading =
value; }
}
private
double
z;//="770.962000000316"
public
double
Z
{
get {
return
z; }
set { z =
value; }
}
private
double
y;//="24483.2877865981"
public
double
Y
{
get {
return
y; }
set { y =
value; }
}
private
double
x;//="10533.2696556843"
public
double
X
{
get {
return
x; }
set { x =
value; }
}
}
2、Xml读取
a、Xml加载
Xml是个标准,对于用该标准存取的内容可以来自文件、内部串或者二进制流,所以对于Xml的加载有这么几种:
加载xml文件
Load(string filename);
加载xml流
Load(Stream inStream);
加载xml字符串
LoadXml(string xml);
b、Xml元素读取
XmlDocument支持使用xpath表达式选择文档中节点,方法:
SelectNodes(String expression)
SelectSingleNode(string expression)
SelectNodes 返回符合expression表达式的所有元素,返回值为XmlNodeList,比如本例子是通过XmlNodeList
nodelist = xmlDoc.SelectNodes("/CameraGroup/Camera");获取所有的Camera节点。
SelectSingleNode只返回第一个符合expression表达式的节点,如果没有返回null值。
返回的XmlNodeList,我们可以通过循环读取,对于单个XmlNode,我们通过Attributes获取属性值。
读取的完整代码如下:
public
static
Hashtable
getCameraXml(string
path)
{
Hashtable
hashtable =
new
Hashtable();
if (File.Exists(path))
{
XmlDocument
xmlDoc =
new
XmlDocument();
//xml来自本地文件
xmlDoc.Load(path);
if (xmlDoc !=
null)
{
//获取所有的Camera节点
XmlNodeList
nodelist = xmlDoc.SelectNodes("/CameraGroup/Camera");
//遍历节点获取节点属性,并保存在LocationCamera类中
foreach
(XmlNode
node
in nodelist)
{
LocationCamera
locationCamera =
new
LocationCamera();
locationCamera.Name=node.Attributes["Name"].Value.ToString();
locationCamera.Roll=System.Convert.ToDouble(node.Attributes["roll"].Value.ToString());
locationCamera.Tilt = System.Convert.ToDouble(node.Attributes["tilt"].Value.ToString());
locationCamera.Heading = System.Convert.ToDouble(node.Attributes["heading"].Value.ToString());
locationCamera.X = System.Convert.ToDouble(node.Attributes["x"].Value.ToString());
locationCamera.Y = System.Convert.ToDouble(node.Attributes["y"].Value.ToString());
locationCamera.Z = System.Convert.ToDouble(node.Attributes["z"].Value.ToString());
hashtable.Add(locationCamera.Name, locationCamera);
Console.WriteLine(node.OuterXml);
}
return
hashtable;
}
}
return
null;
}
SelectNodes、SelectSingleNode也可以读取指定属性值的节点,比如XmlNodeList
nodelist = xmlDoc.SelectNodes("/CameraGroup/Camera[@Name='分区1']");表示读取Name为"分区1"的所有节点。
二、Xml创建的写入
写入内容主要包括xml声明、根节点、子节点及节点属性。生成的Xml文件和代码如下:
<?xml version="1.0"?>
-<CameraGroup WKT="UNKNOWNCS["unnamed"">
<Camera X="112.42342" Name="分区1"/>
</CameraGroup>
写入的代码:
public
static
void
writeCameraXml(string
path)
{
XmlDocument
xmlDoc =
new
XmlDocument();
//创建Xml声明部分,即<?xml version="1.0" encoding="utf-8" ?>
xmlDoc.CreateXmlDeclaration("1.0",
"utf-8",
"yes");
//创建CameraGroup根节点
XmlNode
rootNode = xmlDoc.CreateElement("CameraGroup");
//创建WKT属性
XmlAttribute
wktAttribute = xmlDoc.CreateAttribute("WKT");
wktAttribute.Value =
"UNKNOWNCS[\"unnamed\"";
//为根节点添加属性
rootNode.Attributes.Append(wktAttribute);
//创建Camera子节点
XmlNode
cameraNode = xmlDoc.CreateElement("Camera");
//创建Name属性
XmlAttribute
nameAttribute = xmlDoc.CreateAttribute("Name");
nameAttribute.Value =
"分区1";
//为Camera添加属性
cameraNode.Attributes.Append(nameAttribute);
//创建X属性
XmlAttribute
xAttribute = xmlDoc.CreateAttribute("X");
xAttribute.Value =
"112.42342";
//为Camera添加X属性
cameraNode.Attributes.Append(xAttribute);
//为根节点CameraGroup添加Camera子节点
rootNode.AppendChild(cameraNode);
//为Xml文档添加根元素
xmlDoc.AppendChild(rootNode);
//保存Xml文件
xmlDoc.Save(path);//path为:@"d:\anxiuyun.xml"
}