1.10.1. fejezet, XML DOM és XPath
Beküldte pzoli - 2016, június 2 - 12:33du
namespace XMLDOMProject { class Program { static void Main(string[] args) { XmlDocument xdocr = new XmlDocument(); xdocr.Load(@"c:\temp\persons.xml"); foreach (XmlNode node in xdocr.ChildNodes) { Console.WriteLine(node.Name); } string[] jobs = { "program analyzer", "program tester", "database manager" }; XmlDocument xdocw = new XmlDocument(); XmlElement root = xdocw.CreateElement("jobList"); xdocw.AppendChild(root); XmlNodeChangedEventHandler handler = null; handler = (sender, e) => { Console.WriteLine(e.Node.Value); }; xdocw.NodeInserting += handler; int idx = 1; foreach (string jobName in jobs) { XmlElement element = xdocw.CreateElement("job"); XmlText text = xdocw.CreateTextNode(jobName); XmlNode childNode = root.AppendChild(element); childNode.AppendChild(text); XmlAttribute attr = xdocw.CreateAttribute("id"); attr.Value = (idx++).ToString(); childNode.Attributes.SetNamedItem(attr); } Console.ReadKey(); // https://msdn.microsoft.com/en-us/library/d271ytdx(v=vs.110).aspx // http://www.w3schools.com/xsl/xpath_syntax.asp XmlNodeList nodeList = root.SelectNodes("job[@id>1]"); foreach (XmlNode job in nodeList) { XmlNode id = job.Attributes.GetNamedItem("id"); XmlAttribute attr = xdocw.CreateAttribute("salary"); attr.Value = (int.Parse(id.Value) * 2000).ToString(); job.Attributes.SetNamedItem(attr); Console.WriteLine("New salary: {0}", attr.Value); } xdocw.Save(@"c:\temp\jobs.xml"); Console.ReadKey(); } } }
- A hozzászóláshoz be kell jelentkezni