當前位置:首頁 > IT技術 > Windows編程 > 正文

C#下使用XmlDocument詳解
2021-09-16 11:43:41

轉載:https://blog.csdn.net/mpegfour/article/details/78550228

?

XML在開發(fā)中作為文件存儲格式、數據交換的協(xié)議用的非常普遍,各個編程語言有都支持。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="分區(qū)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表達式選擇文檔中節(jié)點,方法:
SelectNodes(String expression)
SelectSingleNode(string expression)
SelectNodes 返回符合expression表達式的所有元素,返回值為XmlNodeList,比如本例子是通過XmlNodeList nodelist = xmlDoc.SelectNodes("/CameraGroup/Camera");獲取所有的Camera節(jié)點。
SelectSingleNode只返回第一個符合expression表達式的節(jié)點,如果沒有返回null值。
返回的XmlNodeList,我們可以通過循環(huán)讀取,對于單個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節(jié)點
XmlNodeList nodelist = xmlDoc.SelectNodes( "/CameraGroup/Camera" );
//遍歷節(jié)點獲取節(jié)點屬性,并保存在 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也可以讀取指定屬性值的節(jié)點,比如XmlNodeList nodelist = xmlDoc.SelectNodes("/CameraGroup/Camera[@Name='分區(qū)1']");表示讀取Name為"分區(qū)1"的所有節(jié)點。

二、Xml創(chuàng)建的寫入
寫入內容主要包括xml聲明、根節(jié)點、子節(jié)點及節(jié)點屬性。生成的Xml文件和代碼如下:
<?xml version="1.0"?>
-<CameraGroup WKT="UNKNOWNCS["unnamed"">
<Camera X="112.42342" Name="分區(qū)1"/>
</CameraGroup>
寫入的代碼:
public static void writeCameraXml( string path)
{
XmlDocument xmlDoc = new XmlDocument ();
//創(chuàng)建Xml聲明部分,即<?xml version="1.0" encoding="utf-8" ?>
xmlDoc.CreateXmlDeclaration( "1.0" , "utf-8" , "yes" );
//創(chuàng)建CameraGroup根節(jié)點
XmlNode rootNode = xmlDoc.CreateElement( "CameraGroup" );
//創(chuàng)建WKT屬性
XmlAttribute wktAttribute = xmlDoc.CreateAttribute( "WKT" );
wktAttribute.Value = "UNKNOWNCS["unnamed"" ;
//為根節(jié)點添加屬性
rootNode.Attributes.Append(wktAttribute);
//創(chuàng)建Camera子節(jié)點
XmlNode cameraNode = xmlDoc.CreateElement( "Camera" );
//創(chuàng)建Name屬性
XmlAttribute nameAttribute = xmlDoc.CreateAttribute( "Name" );
nameAttribute.Value = "分區(qū)1" ;
//為Camera添加屬性
cameraNode.Attributes.Append(nameAttribute);
//創(chuàng)建X屬性
XmlAttribute xAttribute = xmlDoc.CreateAttribute( "X" );
xAttribute.Value = "112.42342" ;
//為Camera添加X屬性
cameraNode.Attributes.Append(xAttribute);
//為根節(jié)點CameraGroup添加Camera子節(jié)點
rootNode.AppendChild(cameraNode);
//為Xml文檔添加根元素
xmlDoc.AppendChild(rootNode);
//保存Xml文件
xmlDoc.Save(path); //path為:@"d:anxiuyun.xml"
}



本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務立即開通 >