擷取元素屬性有兩個方法,XElement.Attribute() 和XElement.Attributes() 簡單舉個例就知道了 // we will use this to store a reference to one of the elements in the XML tree.XElement firstParticipant; XDocument xDocument = new XDocument( new XElement("BookParticipants",
XDocument xDocument = new XDocument( new XElement("BookParticipants", new XElement("BookParticipant", new XAttribute("type", "Author"), new XElement("FirstName", "Joe"), new XElement("LastName", "Rattz")), new
Student[] students = { new Student { Id = 1, Name = "Joe Rattz" }, new Student { Id = 7, Name = "Anthony Adams" }, new Student { Id = 13, Name = "Stacy Sinclair" }, new Student { Id = 72, Name = "Dignan Stephens" }}; StudentClass[]
XElement.SetElementValue這個方法相當的體貼,它會自動判斷元素是否有該子項目,如果有就是更新,沒有就是增加,那麼刪除呢,也簡單得讓人開心的笑,XElement.SetElementValue("LastName", null),這句是刪除LastName這個元素 // we will use this to store a reference to one of the elements in the XML tree.XElement firstParticipant;
這個發生挺有意思的,有時候我們打亂文件項目原有的順序,比方說對元素順序進行倒轉Reverse但是有時候又需要取回原有的順序,這個時候就可以使用InDocumentOrder,下面是例子 XDocument xDocument = new XDocument( new XElement("BookParticipants", new XElement("BookParticipant", new XAttribute("type", "Author"), new
便利元素屬性有幾個方法XElement.FirstAttribute,XAttribute.NextAttribute,XAttribute.PreviousAttribute,XElement.LastAttribute 舉兩個例子就OK了,其實的用法是一樣的,從字眼上也能看出它們的功能 首先是XElement.FirstAttribute // we will use this to store a reference to one of the elements in the XML