1. Create xml
1 public static void CreateXmlDoc() 2 { 3 XElement myDoc = 4 new XElement("Customers", 5 new XElement("Customer", new XAttribute("ID", "1"), 6 new XElement("Usename", "Rock Zed"), 7 new XElement("FirstName", "Rock"), 8 new XElement("LastName", "Zed"), 9 new XElement("PhoneNo", "35481254") 10 ) 11 ); 12 myDoc.Save("Customer.xml"); 13 }
xml document content created as follows:
<?xml version="1.0" encoding="utf-8"?> <Customers> <Customer ID="1"> <Usename>Rock Zed</Usename> <FirstName>Rock</FirstName> <LastName>Zed</LastName> <PhoneNo>35481254</PhoneNo> </Customer> </Customers>
2. Obtain the XML document content
1 #region Load XML doc 2 public static XDocument GetXmlDoc() 3 { 4 try 5 { 6 XDocument myDoc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/Customer.xml")); 7 return myDoc; 8 } 9 catch (System.IO.FileNotFoundException ex) 10 { 11 return null; 12 } 13 } 14 #endregion 15 // The custom xml content corresponding to the class corresponding to the acquired set of classes 16 public static List <the Customer> GetXmlData () . 17 { 18 is List <the Customer> = listCs new new List <the Customer> (); . 19 the XDocument myDoc = XmlHelper.GetXmlDoc (); 20 is IF (! myDoc = null ) 21 is { 22 is var Query = from C in myDoc.Descendants ( " the Customer " ) 23 is SELECT new new the Customer 24 { 25 ID=c.FirstAttribute.Value, 26 UseName = c.Element("Usename").Value, 27 FirstName = c.Element("FirstName").Value, 28 LastName = c.Element("LastName").Value, 29 Phone = c.Element("PhoneNo").Value 30 }; 31 foreach (var item in query) 32 { 33 listCs.Add(item); 34 } 35 } 36 return listCs; 37 }
Xml document content and attributes above Customer class nodes corresponding to the following Customer class
public class Customer { public string ID { get; set; } public string UseName { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Phone { get; set; } }
3. Add content to XML documents
#region Insert new item public static void InsertNewElement(Customer cs) { try { // Load current document. XDocument myDoc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/Customer.xml")); Random r = new Random(); // add new element XElement newElement = new XElement("Customer",new XAttribute("ID", r.Next(5000)), new XElement("Usename", cs.UseName), new XElement("FirstName", cs.FirstName), new XElement("LastName", cs.LastName), new XElement("PhoneNo", cs.Phone)); myDoc.Descendants("Customers").First().Add(newElement); // Save changes. myDoc.Save(System.Web.HttpContext.Current.Server.MapPath("~/Customer.xml")); } catch (Exception ex) { throw ex; } } #endregion
4. Modify the contents of XML documents
1 public static void EditElement(Customer cs) 2 { 3 try 4 { 5 // Load current document. 6 XDocument myDoc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/Customer.xml")); 7 //by ID load xml 8 IEnumerable<XElement> products = from c in myDoc.Descendants("Customer") where c.FirstAttribute.Value == cs.ID select c; 9 if (products.Count() > 0) 10 { 11 XElement product = products.First(); 12 //replace xml element 13 product.ReplaceNodes( new XElement("Usename", cs.UseName), new XElement("FirstName", cs.FirstName), new XElement("LastName", cs.LastName), new XElement("PhoneNo", cs.Phone)); 14 } 15 myDoc.Save(HttpContext.Current.Server.MapPath("~/Customer.xml")); 16 } 17 catch (Exception ex) 18 { 19 throw ex; 20 } 21 }
5. Delete XML document nodes
1 public static void DeleteElement(string id) 2 { 3 try 4 { 5 XDocument myDoc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/Customer.xml")); 6 IEnumerable<XElement> customer = from c in myDoc.Descendants("Customer") where c.FirstAttribute.Value == id select c; 7 if (customer.Count() > 0) 8 { 9 customer.First().Remove(); 10 } 11 myDoc.Save(HttpContext.Current.Server.MapPath("~/Customer.xml")); 12 } 13 catch (Exception ex) 14 { 15 throw ex; 16 } 17 }
6. specific query XML node content by specifying a value
1 #region Query customer 2 public static Customer FindCustomerByID(string id) 3 { 4 try 5 { 6 Customer cs = new Customer(); 7 // Load current document. 8 XDocument myDoc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/Customer.xml")); 9 var query = from c in myDoc.Root.Elements("Customer") 10 where (string)c.FirstAttribute.Value == id 11 select new Customer 12 { 13 ID = c.FirstAttribute.Value, 14 UseName = c.Element("Usename").Value, 15 FirstName = c.Element("FirstName").Value, 16 LastName = c.Element("LastName").Value, 17 Phone = c.Element("PhoneNo").Value 18 }; 19 20 foreach (var item in query) 21 { 22 cs.ID = item.ID; 23 cs.UseName = item.UseName; 24 cs.FirstName = item.FirstName; 25 cs.LastName = item.LastName; 26 cs.Phone = item.Phone; 27 } 28 return cs; 29 } 30 catch (Exception ex) 31 { 32 return null; 33 } 34 } 35 #endregion
7. The combination of the above methods do a MVC operation deletions XML document to change the source address of the following investigation Demo
https://github.com/ZedH/XmlTest