在前几介绍的XMl 操作时都是要求要求 实体类才能进行处理但有时要求必须对原XML文件进行解析以下是xml 序列化与反序列化的使用方法这次重点介绍一下如何对原生的XML文件进行常用的操作,现在小编就来说说关于net程序的开发环境?下面内容希望能帮助到你,我们来一起看看吧!
net程序的开发环境
在前几介绍的XMl 操作时都是要求要求 实体类才能进行处理。但有时要求必须对原XML文件进行解析。以下是xml 序列化与反序列化的使用方法。这次重点介绍一下如何对原生的XML文件进行常用的操作
c# .net XML 序列化与反序列化-NET FrameWork工具类分享
c# .net XML 工具类封装-NET FrameWork工具类分享
- 将文件转换成XMl对象
XmlDocument doc = new XmlDocument();
doc.Load(@"fileName.xml");
- XmlDocument 保存到文件中
doc.Save(@"fileName.xml");
- 将XML字符串转成 XmlDocument
XmlDocument doc = new XmlDocument();
doc.LoadXml("<noteName></noteName>");
- 读取xml里的内容
XmlNode xn = doc.SelectSingleNode("noteName"); //读取节点
string noteType = xn.GetAttribute("attrName").ToString(); //读取节点属性的值
string value=xn.InnerText;//读取点中的属必值
- 常用读写操作
XmlElement newElement = doc.createElement_x("noteName"); //创建新的节点
//创建属性
XmlAttribute xmlAttribute = doc.CreateAttribute("attrName");
xmlAttribute.InnerText = "5";
newElement.SetAttributeNode(xelType);
//创建子节点
XmlElement xmlElement = doc.createElement_x("NodeName");
xmlElement.InnerText = "Tang";
newElement.AppendChild(xelAuthor);
- 快速查找节点信息
XmlElement xmlElement = xmlDoc.DocumentElement;
string partten = string.Format("/notes/notes[@Type=\"{0}\"]", "11");
XmlElement xmlElementSelected = (XmlElement)xmlElement.SelectSingleNode(partten);
这里的分析规则是XPath