在开发过程中,XML作为文件存储格式、数据交换的协议非常普遍,受各种编程语言的支持。 W3C也制定了XML DOM的标准。 这里主要介绍. Net中的XML文档。 这包括读取和写入xml等功能。一、Xml的加载读取1,准备数据等Xml测试数据: Xml版本=' 1.0 ' encoding=' utf-8 '? - cameragroupwkt=' unknown cs [ ' unnamed ' ] '相机持续时间='5' comment=' ' roll='-4.2937481997575 e-14 ' tilt='-15.333841267255 ' heading='-50.52525255 A254-D7133EB1B7BB.jpg'name='初始接口'/相机持续时间='5' comment=' ' roll='-9.54166404439055 e-15 ' tilt='-12.2364039278614 ' heading='-71.25831444 1C-93C0-DCE573F86B5D.jpg'name='用于存储分区1'//CameraGroup读取Xml分析后的数据。 publicclasslocationcamera { publiclocationcamera { } { }专用名称; 公共字符串name { get { return name; } set { name=value; } }私密int duration; 公共int duration { get { return duration; } set { duration=value; } }私密双角色; //='-4.29374881997575 e-14 ' publicdoubleroll { get { return roll; } set { roll=value; } }私密双标题; //='-15.333841267255 ' publicdoubletilt { get { return tilt; } set { tilt=value; } }私密双面读写; //='-50.5252574662688 ' publicdoubleheading { get { return heading }; 设置{ heading=value; } }私密双面z; //=' 770.962000000316 '公共双精度z { get { returnz; } set { z=value; } }私密双面y; //=' 24483.2877865981 ' public doubley { get { returny; } set { y=value; } }私密双面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" }