MyXML.xml
<?xml version="1.0" encoding="utf-8" ?>
<!-- 根节点 子节点 父节点 -->
<!-- 根节点有且只有一个 -->
<Students>
<Student id="1">
<Name>zs</Name>
<Age>16</Age>
<Sex>男</Sex>
</Student>
<Student id="2">
<Name>ls</Name>
<Age>17</Age>
<Sex>男</Sex>
</Student>
<Student id="3">
<Name>ww</Name>
<Age>19</Age>
<Sex>男</Sex>
</Student>
<Student id="4">
<Name>zs</Name>
<Age>20</Age>
<Sex>男</Sex>
</Student>
<Student id="5">
<Name>aa</Name>
<Age>21</Age>
<Sex>女</Sex>
</Student>
</Students>
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Teilu
{
class Program
{
static void Main(string[] args)
{
#region 查询
//XmlDocument xmldocument = new XmlDocument();
//xmldocument.Load("XML.xml");
//XmlNodeList Tielu = xmldocument.GetElementsByTagName("Tielu");
//XmlNodeList tiedao = Tielu[0].ChildNodes;
//foreach (XmlNode item in tiedao)
//{
// Console.WriteLine("尊敬的乘客您好,您的车次为{0}", item.Attributes["id"] == null ? "此车次信息尚未完善" : item.Attributes["id"].Value);
// foreach (XmlNode item2 in item.ChildNodes)
// {
// Console.WriteLine(item2.InnerText);
// }
//}
#endregion
#region 创建,增加
//XmlDocument doc = new XmlDocument();
//doc.Load("Xml.xml");
//XmlElement Tiedao = doc.CreateElement("Teidao");
//XmlElement shizhong = doc.CreateElement("Shizhong");
//shizhong.InnerText = "长沙南-上海虹桥4";
//XmlElement time = doc.CreateElement("Time");
//time.InnerText = "13:32-19:39";
//XmlElement lishi = doc.CreateElement("Lishi");
//lishi.InnerText = "用时05:39,当日到达";
//Tiedao.AppendChild(shizhong);
//Tiedao.AppendChild(time);
//Tiedao.AppendChild(lishi);
//XmlAttribute id = doc.CreateAttribute("id");
//id.Value = "G1774";
//Tiedao.Attributes.Append(id);
//doc.DocumentElement.AppendChild(Tiedao);
//doc.Save("Xml.xml");
#endregion
#region 删除
//XmlDocument doc = new XmlDocument();
//doc.Load("Xml.xml");
//XmlNodeList xmlnodelist = doc.SelectNodes("/Tielu/Teidao/Shizhong");
//foreach (XmlNode item in xmlnodelist)
//{
// if (item.InnerText.Equals("长沙南-上海虹桥3"))
// {
// item.RemoveAll();
// break;
// }
//}
//doc.Save("Xml.xml");
#endregion
#region 修改
//XmlDocument doc = new XmlDocument();
//doc.Load("Xml.xml");
//XmlNodeList xmlnode = doc.SelectNodes("/Tielu/Teidao/Shizhong");
//{
// foreach (XmlNode item in xmlnode)
// if (item.InnerText.Equals("长沙南-上海虹桥1"))
// {
// item.InnerText = "长沙南-杭州北";
// break;
// }
//}
//doc.Save("Xml.xml");
#endregion
Console.ReadKey();
}
}
}
XML.xml
<?xml version="1.0" encoding="utf-8" ?>
<Tielu>
<Teidao id ="G1354">
<Shizhong>长沙南-上海虹桥1</Shizhong>
<Time>11:40-17:03</Time>
<Lishi>用时05:23,当日到达</Lishi>
</Teidao>
<Teidao id ="G1322">
<Shizhong>长沙南-上海虹桥2</Shizhong>
<Time>12:49-18:22</Time>
<Lishi>用时05:33,当日到达</Lishi>
</Teidao>
<Teidao id ="G1362">
<Shizhong>长沙南-上海虹桥3</Shizhong>
<Time>13:25-19:04</Time>
<Lishi>用时06:07,当日到达</Lishi>
</Teidao>
</Tielu>
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Base13
{
class Program
{
static void Main(string[] args)
{
//XML和JSON格式之间的差别
//JSON 前台和后台进行数据交互的时候
//JSON 1 单个对象{属性名称:值,属性名称2:值2}
//JSON 2 多个对象[{属性名称:值,属性名称:值2},{属性名称:值,属性名称2:值2}]
//弊端? JSON 格式是一个键值对的string(字符串)
//字符串有长度的限制 存在于网页之间交互
//获取到天气预报信息 湖南气象局 中央气象局
//XMl进行数据的传递(WEB SERVICE)
//C# java C C++
#region xml读取01
//读取XML文件
//XmlDocument xmldocument = new XmlDocument();
//xmldocument.Load(@"MyXml.XML");
得到根节点
//XmlNode Students = xmldocument.SelectSingleNode("Students");
得到所有的子的节点
//XmlNodeList student = Students.ChildNodes;
读取子节点的信息
//foreach (XmlNode item in student)
//{
// //if (item.Attributes["id"] != null)
// //{
// // Console.WriteLine("属性值ID为{0}", item.Attributes["id"].Value);
// //}
// Console.WriteLine("属性值ID为{0}", item.Attributes["id"] == null ? "无" : item.Attributes["id"].Value);
// foreach (XmlNode item2 in item.ChildNodes)
// {
// Console.WriteLine("节点名称:{0},节点值:{1}", item2.Name, item2.InnerText);
// }
//}
#endregion
#region xml读取02
//XmlDocument doc = new XmlDocument();
//doc.Load("MyXML.xml");
得到所有的节点
//XmlNodeList root = doc.ChildNodes;
//foreach (XmlNode item in root)
//{
// if (item.NodeType == XmlNodeType.Element)
// {
// foreach (XmlNode xmlnode in item.ChildNodes)
// {
// Console.WriteLine(xmlnode.Attributes["id"].Value);
// foreach (XmlNode c in xmlnode.ChildNodes)
// {
// //as 强制转换
// XmlElement xmle = c as XmlElement;
// Console.WriteLine(xmle.InnerText);
// }
// }
// }
//}
#endregion
#region xml读取03
//XmlDocument doc = new XmlDocument();
//doc.Load("MyXML.xml");
//XmlNodeList Students = doc.GetElementsByTagName("Students");
//XmlNodeList student = Students[0].ChildNodes;
//foreach (XmlNode item in student)
//{
// Console.WriteLine("属性值ID为{0}", item.Attributes["id"] == null ? "无" : item.Attributes["id"].Value);
// foreach (XmlNode item2 in item.ChildNodes)
// {
// Console.WriteLine("节点名称:{0},节点值:{1}", item2.Name, item2.InnerText);
// }
//}
#endregion
#region xml读取04 xpath
//XmlDocument doc = new XmlDocument();
//doc.Load("MyXML.xml");
//XmlNodeList names = doc.SelectNodes("/Students/Student");
//foreach (XmlNode item in names)
//{
// foreach (XmlNode item2 in item.ChildNodes)
// {
// Console.WriteLine(item2.InnerText);
// }
//}
//XmlDocument doc = new XmlDocument();
//doc.Load("MyXML.xml");
//XmlNode xn = doc.SelectSingleNode("/Students/Student/Name");
//Console.WriteLine(xn.InnerText);
#endregion
#region 创建一个节点
//XmlDocument doc = new XmlDocument();
//doc.Load("MyXML.xml");
//XmlElement Student = doc.CreateElement("Student");
创建Student子节点
//XmlElement name = doc.CreateElement("Name");
//name.InnerText = "新加入的值";
//XmlElement age = doc.CreateElement("Age");
//age.InnerText = "22";
//XmlElement sex = doc.CreateElement("Sex");
//sex.InnerText = "男";
//Student.AppendChild(name);
//Student.AppendChild(age);
//Student.AppendChild(sex);
追加属性值
//XmlAttribute id = doc.CreateAttribute("id");
//id.Value = "6";
//Student.Attributes.Append(id);
在根节点下面追加一个节点
//doc.DocumentElement.AppendChild(Student);
保存
//doc.Save("MyXMl.xml");
#endregion
#region 删除一个节点
//XmlDocument doc = new XmlDocument();
//doc.Load("MyXML.xml");
//XmlNodeList xmlnodelist = doc.SelectNodes("/Students/Student/Name");
//foreach (XmlNode item in xmlnodelist)
//{
// if (item.InnerText.Equals("新加入的值"))
// {
// item.RemoveAll();
// break;
// }
//}
//doc.Save("MyXML.xml");
//XmlNodeList xmlnodelist = doc.SelectNodes("/Students/Student");
//foreach (XmlNode item in xmlnodelist)
//{
// if (item.Attributes[0].Name.Equals("id"))
// {
// item.Attributes.Remove(item.Attributes[0]);
// }
//}
//doc.Save("MyXML.xml");
#endregion
#region 修改某一个属性的值
//XmlDocument doc = new XmlDocument();
//doc.Load("MyXML.xml");
//XmlNodeList xmlnode = doc.SelectNodes("/Students/Student/Name");
//foreach (XmlNode item in xmlnode)
//{
// if (item.InnerText.Equals("zs"))
// {
// item.InnerText = "张三1";
// break;
// }
//}
//doc.Save("MyXML.xml");
#endregion
#region 替换节点所有的内容
//XmlDocument doc = new XmlDocument();
//doc.Load("MyXML.xml");
创建一个新的节点
//XmlElement Student = doc.CreateElement("Student");
创建Student子节点
//XmlElement name = doc.CreateElement("Name");
//name.InnerText = "新加入的值";
//XmlElement age = doc.CreateElement("Age");
//age.InnerText = "22";
//XmlElement sex = doc.CreateElement("Sex");
//sex.InnerText = "男";
//Student.AppendChild(name);
//Student.AppendChild(age);
//Student.AppendChild(sex);
追加属性值
//XmlAttribute id = doc.CreateAttribute("id");
//id.Value = "6";
//Student.Attributes.Append(id);
doc.DocumentElement.ReplaceChild(Student, doc.DocumentElement.FirstChild);
找到旧的节点
//XmlNodeList xmlnode = doc.SelectNodes("/Students/Student/Name");
//foreach (XmlNode item in xmlnode)
//{
// if (item.InnerText.Equals("zs"))
// {
// // doc.ReplaceChild(Student, item);
// doc.DocumentElement.ReplaceChild(Student, item.ParentNode);
// }
//}
//doc.Save("MyXML.xml");
#endregion
#region 修改属性值
//XmlDocument doc = new XmlDocument();
//doc.Load("MyXML.xml");
//XmlNode xmlnode = doc.DocumentElement.FirstChild;
//xmlnode.Attributes[0].Value = "11";
//doc.Save("MyXML.xml");
#endregion
Console.ReadKey();
}
}
}