C#之xml操作

版权声明:博主原创文章,转载注明出处! https://blog.csdn.net/xmh19936688/article/details/52164902

创建XML文档对象
XmlDocument xmlDoc = new XmlDocument();
创建根节点
XmlElement root = xmlDoc.CreateElement("root");
xmlDoc.AppendChild(root);
读取根节点
XmlElement root = xmlDoc.DocumentElement;
根据名称获取子节点
(XmlElement)ele.GetElementsByTagName("city");//子节点列表
(XmlElement)ele.GetElementsByTagName("city").Item(0);//第一个“city”子节点
currentRoot.FirstChild;//第一个子节点
创建普通节点
XmlElement eleA= xmlDoc.CreateElement("ele");
eleB.applendChild(eleA);
给节点设置属性
root.SetAttribute("aa", "bb");//<ele aa="bb"/>
获取指定节点的某个属性值
ele.GetAttribute("city");//<E city="bj"/>
保存xml
xmlDoc.Save(xmlFilePath);

猜你喜欢

转载自blog.csdn.net/xmh19936688/article/details/52164902