當(dāng)前位置:首頁 > IT技術(shù) > Windows編程 > 正文

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

轉(zhuǎn)載:https://blog.csdn.net/mpegfour/article/details/78550228

?

XML在開發(fā)中作為文件存儲(chǔ)格式、數(shù)據(jù)交換的協(xié)議用的非常普遍,各個(gè)編程語言有都支持。W3C也制定了XML DOM的標(biāo)準(zhǔn)。在這里主要介紹下.Net中的XmlDocument,包括xml讀取和寫入等功能。
一、Xml的加載讀取
1、數(shù)據(jù)等準(zhǔn)備
Xml測(cè)試數(shù)據(jù):
<?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>
讀取的數(shù)據(jù),我們定義了一個(gè)實(shí)體類LocationCamera,用來保存Xml解析后的數(shù)據(jù):
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是個(gè)標(biāo)準(zhǔn),對(duì)于用該標(biāo)準(zhǔn)存取的內(nèi)容可以來自文件、內(nèi)部串或者二進(jìn)制流,所以對(duì)于Xml的加載有這么幾種:
加載xml文件
Load(string filename);
加載xml流
Load(Stream inStream);
加載xml字符串
LoadXml(string xml);
b、Xml元素讀取
XmlDocument支持使用xpath表達(dá)式選擇文檔中節(jié)點(diǎn),方法:
SelectNodes(String expression)
SelectSingleNode(string expression)
SelectNodes 返回符合expression表達(dá)式的所有元素,返回值為XmlNodeList,比如本例子是通過XmlNodeList nodelist = xmlDoc.SelectNodes("/CameraGroup/Camera");獲取所有的Camera節(jié)點(diǎn)。
SelectSingleNode只返回第一個(gè)符合expression表達(dá)式的節(jié)點(diǎn),如果沒有返回null值。
返回的XmlNodeList,我們可以通過循環(huán)讀取,對(duì)于單個(gè)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é)點(diǎn)
XmlNodeList nodelist = xmlDoc.SelectNodes( "/CameraGroup/Camera" );
//遍歷節(jié)點(diǎn)獲取節(jié)點(diǎn)屬性,并保存在 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é)點(diǎn),比如XmlNodeList nodelist = xmlDoc.SelectNodes("/CameraGroup/Camera[@Name='分區(qū)1']");表示讀取Name為"分區(qū)1"的所有節(jié)點(diǎn)。

二、Xml創(chuàng)建的寫入
寫入內(nèi)容主要包括xml聲明、根節(jié)點(diǎn)、子節(jié)點(diǎn)及節(jié)點(diǎn)屬性。生成的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é)點(diǎn)
XmlNode rootNode = xmlDoc.CreateElement( "CameraGroup" );
//創(chuàng)建WKT屬性
XmlAttribute wktAttribute = xmlDoc.CreateAttribute( "WKT" );
wktAttribute.Value = "UNKNOWNCS["unnamed"" ;
//為根節(jié)點(diǎn)添加屬性
rootNode.Attributes.Append(wktAttribute);
//創(chuàng)建Camera子節(jié)點(diǎn)
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é)點(diǎn)CameraGroup添加Camera子節(jié)點(diǎn)
rootNode.AppendChild(cameraNode);
//為Xml文檔添加根元素
xmlDoc.AppendChild(rootNode);
//保存Xml文件
xmlDoc.Save(path); //path為:@"d:anxiuyun.xml"
}



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

開通會(huì)員,享受整站包年服務(wù)立即開通 >