using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; namespace Fuel01 { /***************** * http://www.dotnetcurry.com/showarticle.aspx?ID=564 * http://broadcast.oreilly.com/2010/10/understanding-c-simple-linq-to.html *******************/ public partial class xmlvrac : Form { XDocument doc; XElement xelement; IEnumerable employees; public xmlvrac() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SaveDataToAnXmlFile(@"c:\tmp\test.Xml"); } private XDocument GetStarbuzzData() { /* * You can use an XDocument to create an XML file, and that includes XML * files you can read and write using DataContractSerializer. * * An XMLDocument object represents an XML document. It's part of the * System.Xml.Linq namespace. * * Use XElement objects to create elements under the XML tree. */ XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XComment("Starbuzz Customer Loyalty Data"), new XElement("starbuzzData", new XAttribute("storeName", "Park Slope"), new XAttribute("location", "Brooklyn, NY"), new XElement("person", new XElement("personalInfo", new XElement("name", "Janet Venutian"), new XElement("zip", 11215)), new XElement("favoriteDrink", "Choco Macchiato"), new XElement("moneySpent", 255), new XElement("visits", 50)), new XElement("person", new XElement("personalInfo", new XElement("name", "Liz Nelson"), new XElement("zip", 11238)), new XElement("favoriteDrink", "Double Cappuccino"), new XElement("moneySpent", 150), new XElement("visits", 35)), new XElement("person", new XElement("personalInfo", new XElement("name", "Matt Franks"), new XElement("zip", 11217)), new XElement("favoriteDrink", "Zesty Lemon Chai"), new XElement("moneySpent", 75), new XElement("visits", 15)), new XElement("person", new XElement("personalInfo", new XElement("name", "Joe Ng"), new XElement("zip", 11217)), new XElement("favoriteDrink", "Banana Split in a Cup"), new XElement("moneySpent", 60), new XElement("visits", 10)), new XElement("person", new XElement("personalInfo", new XElement("name", "Sarah Kalter"), new XElement("zip", 11215)), new XElement("favoriteDrink", "Boring Coffee"), new XElement("moneySpent", 110), new XElement("visits", 15)))); return doc; } private void SaveDataToAnXmlFile(string filename) { doc = GetStarbuzzData(); doc.Save(filename); } private void QueryTheData(XDocument doc) { // Do a simple query and print the results to the console var data = from item in doc.Descendants("person") select new { drink = item.Element("favoriteDrink").Value, moneySpent = item.Element("moneySpent").Value, zipCode = item.Element("personalInfo").Element("zip").Value }; foreach (var p in data) outTxt.AppendText(p.ToString() + "\r\n"); // Do a more complex query and print the results to the console var zipcodeGroups = from item in doc.Descendants("person") group item.Element("favoriteDrink").Value by item.Element("personalInfo").Element("zip").Value into zipcodeGroup select zipcodeGroup; foreach (var group in zipcodeGroups) outTxt.AppendText(string.Format("{0} favorite drinks in {1}\r\n", group.Distinct().Count(), group.Key)); } private void button2_Click(object sender, EventArgs e) { foreach (var employee in employees) { outTxt.AppendText(employee.ToString()); } } private void button3_Click(object sender, EventArgs e) { xelement = XElement.Load(@"c:\tmp\employes.xml"); employees = xelement.Elements(); } private void write_one() { } private void button4_Click(object sender, EventArgs e) { var addresses = from address in xelement.Elements("Employee") where (string)address.Element("Address").Element("City") == "Paris" select address; foreach (XElement employee in addresses) { if (employee.Element("Name").Value == "Sam") employee.Element("Sex").ReplaceNodes("Petit"); } } private void button6_Click(object sender, EventArgs e) { xelement.Save(@"c:\tmp\Employes.xml"); } private void button5_Click(object sender, EventArgs e) { xelement.Add(new XElement("Employee", new XElement("EmpId", 5), new XElement("Name", "George"), new XElement("Sex", "Male"), new XElement("Phone", "423-555-4224", new XAttribute("Type", "Home")), new XElement("Phone", "424-555-0545", new XAttribute("Type", "Work")), new XElement("Address", new XElement("Street", "Fred Park, East Bay"), new XElement("City", "Paris"), new XElement("State", "CA"), new XElement("Zip", "95220"), new XElement("Country", "USA")))); } } }