<Teams>
<Member team="U-Shop" Description="U-Shop團(tuán)隊(duì)">
<name>靈動(dòng)生活</name>
<position>planning</position>
</Member>
<Member team="U-Shop" Description="U-Shop團(tuán)隊(duì)">
<name>藍(lán)藍(lán)海天</name>
<position>support</position>
</Member>
<Member team="U-Shop" Description="U-Shop團(tuán)隊(duì)">
<name>神舟龍</name>
<position>lean</position>
</Member>
</Teams>
確定路徑:
string myXmlFilePath = "E:\\U-Shop.xml";
依照此模板我們有c#代碼進(jìn)行建立,方法代碼如下:
//生成xml 結(jié)構(gòu)
private static void GenerateXMLFIL(string XMLfilePath)
{
try
{
//實(shí)例化xml文檔
XmlDocument myXmlDoc = new XmlDocument();
//實(shí)例化一個(gè)根節(jié)點(diǎn)
XmlElement rootElement = myXmlDoc.CreateElement("Teams");
//把根節(jié)點(diǎn)添加到xml文檔中
myXmlDoc.AppendChild(rootElement);
//創(chuàng)建一個(gè)一級(jí)節(jié)點(diǎn)
XmlElement fistLevelElement1 = myXmlDoc.CreateElement("Member");
//設(shè)置屬性
fistLevelElement1.SetAttribute("team", "U-Shop");
fistLevelElement1.SetAttribute("Description", "U-Shop團(tuán)隊(duì)");
//將一級(jí)節(jié)點(diǎn)添加到根節(jié)點(diǎn)
rootElement.AppendChild(fistLevelElement1);
//創(chuàng)建二級(jí)節(jié)點(diǎn)
XmlElement secondLevelElement11 = myXmlDoc.CreateElement("name");
//設(shè)置二級(jí)節(jié)點(diǎn)的串聯(lián)值
secondLevelElement11.InnerText = "靈動(dòng)生活";
//把二級(jí)節(jié)點(diǎn)添加到一級(jí)幾點(diǎn)
fistLevelElement1.AppendChild(secondLevelElement11);
XmlElement threeLevelElement12 = myXmlDoc.CreateElement("position");
threeLevelElement12.InnerText = "planning";
fistLevelElement1.AppendChild(threeLevelElement12);
XmlElement secondLevelElement2 = myXmlDoc.CreateElement("Member");
secondLevelElement2.SetAttribute("team", "U-Shop");
secondLevelElement2.SetAttribute("Description", "U-Shop團(tuán)隊(duì)");
//添加到根節(jié)點(diǎn)
rootElement.AppendChild(secondLevelElement2);
XmlElement secondLevelElement21 = myXmlDoc.CreateElement("name");
secondLevelElement21.InnerText = "藍(lán)藍(lán)海天";
secondLevelElement2.AppendChild(secondLevelElement21);
XmlElement threeLevelElement22 = myXmlDoc.CreateElement("position");
threeLevelElement22.InnerText = "support";
secondLevelElement2.AppendChild(threeLevelElement22);
XmlElement secondLevelElement3 = myXmlDoc.CreateElement("Member");
secondLevelElement3.SetAttribute("team", "U-Shop");
secondLevelElement3.SetAttribute("Description", "U-Shop團(tuán)隊(duì)");
//添加到根節(jié)點(diǎn)
rootElement.AppendChild(secondLevelElement3);
XmlElement secondLevelElement31 = myXmlDoc.CreateElement("name");
secondLevelElement31.InnerText = "神舟龍";
secondLevelElement3.AppendChild(secondLevelElement31);
XmlElement threeLevelElement32 = myXmlDoc.CreateElement("position");
threeLevelElement32.InnerText = "lean";
secondLevelElement3.AppendChild(threeLevelElement32);
//保存,重要
myXmlDoc.Save(XMLfilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
下面的方法就是獲取已經(jīng)創(chuàng)建好的xml文件中信息,方法代碼如下:
//獲取xml信息
private static void GetXMLInformation(string XMLfilePath)
{
//實(shí)例化一個(gè)xml文檔
XmlDocument myXmlDoc = new XmlDocument();
//加載xml文檔
myXmlDoc.Load(XMLfilePath);
//獲得第一個(gè)為Computers的根節(jié)點(diǎn)
XmlNode rootNode = myXmlDoc.SelectSingleNode("Teams");
//獲取根節(jié)點(diǎn)中串聯(lián)值
string innertext = rootNode.InnerText.ToString();
//獲取根節(jié)點(diǎn)雖有子節(jié)點(diǎn)結(jié)構(gòu)
string innerXmlInfo = rootNode.InnerXml.ToString();
//獲取根節(jié)點(diǎn)在內(nèi)的整個(gè)xml的結(jié)構(gòu)
string outXmlInfo = rootNode.OuterXml.ToString();
//獲取根節(jié)點(diǎn)的所有自己點(diǎn)
// }
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
//遍歷所有子節(jié)點(diǎn)
foreach (XmlNode node in firstLevelNodeList)
{
string inner = node.InnerXml;
string outx = node.OuterXml;
//獲取所有子節(jié)點(diǎn)的屬性
XmlAttributeCollection attributeCol = node.Attributes;
//遍歷自己點(diǎn)屬性
foreach (XmlAttribute attri in attributeCol)
{
//獲取每個(gè)節(jié)點(diǎn)屬性的key,value值
string name = attri.Name;
string value = attri.Value;
Console.WriteLine("{0}={1}", name, value);
}
//判斷子節(jié)點(diǎn)是否還有子節(jié)點(diǎn)
if (node.HasChildNodes)
{//如果有子節(jié)點(diǎn)則執(zhí)行
//獲取子節(jié)點(diǎn)中的第一個(gè)孩子
XmlNode secondeLevelNode1 = node.FirstChild;
//得到第一個(gè)孩子的名稱
string name = secondeLevelNode1.Name;
string innetext = secondeLevelNode1.InnerText.ToString();
string outxml = secondeLevelNode1.OuterXml.ToString();
//獲得第一個(gè)孩子的串聯(lián)值
string innerxml = secondeLevelNode1.InnerXml.ToString();
Console.WriteLine("{0}={1}", name, innerxml);
XmlNode secondLevelNode2 = node.ChildNodes[1];
name = secondLevelNode2.Name;
innerxml = secondLevelNode2.InnerXml;
Console.WriteLine("{0}={1}", name, innerxml);
}
}
}
我們很容易獲得上邊代碼中紅色字體的值,即innertext=“靈動(dòng)生活planning藍(lán)藍(lán)海天support神舟龍lean:“理解:根節(jié)點(diǎn)以下所有節(jié)點(diǎn)的值,innerXmlInfo =“
<Member team="U-Shop" Description="U-Shop團(tuán)隊(duì)">
<name>靈動(dòng)生活</name>
<position>planning</position>
</Member>
<Member team="U-Shop" Description="U-Shop團(tuán)隊(duì)">
<name>藍(lán)藍(lán)海天</name>
<position>support</position>
</Member>
<Member team="U-Shop" Description="U-Shop團(tuán)隊(duì)">
<name>神舟龍</name>
<position>lean</position>
</Member>
理解:除根節(jié)點(diǎn)標(biāo)簽后,根節(jié)點(diǎn)包含的xml結(jié)構(gòu)”, outXmlInfo =”
<Teams>
<Member team="U-Shop" Description="U-Shop團(tuán)隊(duì)">
<name>靈動(dòng)生活</name>
<position>planning</position>
</Member>
<Member team="U-Shop" Description="U-Shop團(tuán)隊(duì)">
<name>藍(lán)藍(lán)海天</name>
<position>support</position>
</Member>
<Member team="U-Shop" Description="U-Shop團(tuán)隊(duì)">
<name>神舟龍</name>
<position>lean</position>
</Member>
</Teams>
理解:包含根節(jié)點(diǎn)標(biāo)簽在內(nèi)的xml結(jié)構(gòu)“
innertext :可以把這個(gè)單詞分開進(jìn)行理解,分開就是inner和text,inner就是里面的意思,這里我們可以理解為不包含本節(jié)點(diǎn),即innertext所在節(jié)點(diǎn)的子節(jié)點(diǎn),text就是文本的意思,所以innertext不會(huì)出現(xiàn)標(biāo)簽,整體理解就是innertext所在節(jié)點(diǎn)所包含的子節(jié)點(diǎn)的值;
innerxml:同樣我們也可以把這個(gè)單詞拆分進(jìn)行理解,分開就是inner和xml,inner就是里面的意思,這里我們可以理解為不包含本節(jié)點(diǎn),即innerxml所在節(jié)點(diǎn)的子節(jié)點(diǎn),xml 這里可以認(rèn)為是我們所寫的標(biāo)簽,當(dāng)然也包括它的屬性值,所以這里理解就是innerxml所在節(jié)點(diǎn)所包含的子節(jié)點(diǎn)的標(biāo)簽結(jié)構(gòu);
outerxml:同樣我們也可以把這個(gè)單詞拆分進(jìn)行理解,分開就是outer和xml,out就是外面的意思,這里我們可以理解為包含本節(jié)點(diǎn),即outerxml所在節(jié)點(diǎn)的子節(jié)點(diǎn),xml 這里可以認(rèn)為是我們所寫的標(biāo)簽,當(dāng)然也包括它的屬性值,所以這里理解就是outerxml所在節(jié)點(diǎn)以及所包含的子節(jié)點(diǎn)的標(biāo)簽結(jié)構(gòu)、屬性、值(包含所在節(jié)點(diǎn));