便利元素属性有几个方法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 tree. XElement firstParticipant; XDocument xDocument = new XDocument( new XElement("BookParticipants", firstParticipant = new XElement("BookParticipant", new XAttribute("type", "Author"), new XAttribute("experience", "first-time"), new XAttribute("language", "English"), new XElement("FirstName", "Joe"), new XElement("LastName", "Rattz")))); Console.WriteLine(firstParticipant.FirstAttribute);
输出
type="Author"
然后是
XAttribute.NextAttribute
// 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", firstParticipant = new XElement("BookParticipant", new XAttribute("type", "Author"), new XAttribute("experience", "first-time"), new XAttribute("language", "English"), new XElement("FirstName", "Joe"), new XElement("LastName", "Rattz")))); Console.WriteLine(firstParticipant.FirstAttribute.NextAttribute);
输出
experience="first-time"