您的位置:首页 > 编程语言 > C#

C#读写取Xml文件

2015-09-09 17:30 337 查看
1.读Xml

XmlDocument doc = new XmlDocument();

doc.Load(xmlFile); //加载Xml文件

XmlElement rootElem = doc.DocumentElement; //获取根节点

XmlNodeList infoNodes = rootElem.GetElementsByTagName("s1");

//遍历 Node

foreach (XmlNode node in infoNodes)

{

string strName = ((XmlElement)node).GetAttribute("name");

}

2.写Xml

XmlDocument xmldoc = null;

XmlElement root = null;

xmldoc = new XmlDocument();

root = xmldoc.CreateElement("book");

XmlElement Xlem = xmldoc.CreateElement(“s1”);

Xlem .SetAttribute("name", "t");

root .AppendChild(Xlem);

xmldoc.Save("1.xml");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: