1.
<?xml version="1.0"?>
<aa>
<Answer Status="NORM" Roam="TRUE">
<Tel>13971629664</Tel>
<Lon>43257804</Lon>
<Lat>10901138</Lat>
<RoamCity>杭州市区</RoamCity>
</Answer>
</aa>
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("aaa.xml"));
XmlNode xn=xmlDoc.SelectSingleNode("aa");
XmlNodeList xnl=xn.ChildNodes;
foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("Status"));//显示属性值
Console.WriteLine(xe.GetAttribute("Roam"));
XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
if(xn2.Name == "Tel")
Console.WriteLine(xn2.InnerText);//显示子节点点文本
break;
}
}
2.
XmlDocument doc=new XmlDocument ();
doc.Load ("E:/bb.xml");
XmlNodeList xl=doc.GetElementsByTagName ("Tel");
if(xl.Count >0)
{
Response.Write (xl[0].InnerText );
} (此例同上)
3.
private XmlDocument xmlDocument;
private XmlNode configuration;
xmlDocument = new XmlDocument();
xmlDocument.Load("Config.xml");
configuration = xmlDocument["configuration"];
string str=configuration["Ip"].InnerText;
Config.xml的内容为:
<?xml version="1.0" encoding="gb2312"?>
<configuration>
<Ip>192.168.0.122</Ip>
<Password>pursuer</Password>
</configuration>
4.
private void frmMain_Load(object sender, System.EventArgs e)
{
xml=new XmlDocument();
XmlTextReader reader = new XmlTextReader("multilanguage2.xml");
reader.WhitespaceHandling = WhitespaceHandling.None;
reader.Read();
xml.Load(reader);
reader.Close();
//声明基节点对象
TreeNode RootNode=new TreeNode();
//声明2个临时节点对象,增加时使用
TreeNode tempNode1=new TreeNode();
TreeNode tempNode2=new TreeNode();
//基节点增入treeview控件中
RootNode.Text="基点";
RootNode.Tag="1Root";
treeView1.Nodes.Add(RootNode);
RootNode=treeView1.Nodes[0];
int i,j;
i=0;
//从xml对象中读出第一层元素,逐个增加到treeview控件中
foreach (XmlNode FNode in xml.LastChild){
//声明基节点下的第一层节点对象
TreeNode SubNodeFirst=new TreeNode();
SubNodeFirst.Text=FNode.Attributes["desc"].Value.ToString();
//在该层节点的tag属性中加入层数
SubNodeFirst.Tag="2"+FNode.Attributes["name"].Value.ToString();
RootNode.Nodes.Add(SubNodeFirst);
tempNode1=treeView1.Nodes[0].LastNode;
j=0;
//从xml对象中读出第i个第一层元素的子元素,逐个增加到treeview控件中
foreach(XmlNode SNode in xml.LastChild.ChildNodes[i]){
//声明基节点下的第二层节点对象
TreeNode SubNodeSecond=new TreeNode();
//在该层节点的tag属性中加入层数
SubNodeSecond.Tag="3"+SNode.Attributes["name"].Value.ToString();
SubNodeSecond.Text=SNode.Attributes["desc"].Value.ToString();
tempNode1.Nodes.Add(SubNodeSecond);
tempNode2=treeView1.Nodes[0].LastNode.LastNode;
//从xml对象中读出第i个第一层节点下的第j个元素下的子元素,逐个增加到treeview控件中
foreach(XmlNode TNode in xml.LastChild.ChildNodes[i].ChildNodes[j]){
TreeNode SubNodeThird=new TreeNode();
//声明基节点下的第三层节点对象
SubNodeThird.Text=TNode.Attributes["desc"].Value.ToString();
//在该层节点的tag属性中加入层数
SubNodeThird.Tag="4"+TNode.Attributes["name"].Value.ToString();
tempNode2.Nodes.Add(SubNodeThird);
}
j++;
}
i++;
}
}