Created
April 2, 2013 19:23
-
-
Save Rcomian/5295374 to your computer and use it in GitHub Desktop.
Xml in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://msdn.microsoft.com/en-us/library/azsy1tw2.aspx | |
using System; | |
using System.IO; | |
using System.Xml; | |
public class Sample | |
{ | |
public static void Main() | |
{ | |
// Create the XmlDocument. | |
XmlDocument doc = new XmlDocument(); | |
doc.LoadXml("input.xml"); | |
// Save the document to a file. | |
doc.Save("data.xml"); | |
} | |
} | |
// http://msdn.microsoft.com/en-us/library/d271ytdx.aspx | |
// Load the document and set the root element. | |
XmlDocument doc = new XmlDocument(); | |
doc.Load("bookstore.xml"); | |
XmlNode root = doc.DocumentElement; | |
// Add the namespace. | |
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); | |
nsmgr.AddNamespace("bk", "urn:newbooks-schema"); | |
// Select all nodes where the book price is greater than 10.00. | |
XmlNodeList nodeList = root.SelectNodes( | |
"descendant::bk:book[bk:price>10.00]", nsmgr); | |
foreach (XmlNode book in nodeList) | |
{ | |
// Discount prices by 10%. | |
double price; | |
price = Math.Round(Convert.ToSingle( | |
book.LastChild.InnerText) * 0.9, 2); | |
book.LastChild.InnerText = price.ToString(); | |
} | |
// Display the updated document. | |
doc.Save(Console.Out); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment