Calling the First Attributes Prototype : Attribute « XML LINQ « C# / C Sharp






Calling the First Attributes Prototype

  

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {
        XDocument xDocument = new XDocument(
         new XElement("Books",
           new XElement("Book",
            new XAttribute("type", "Author"),
            new XElement("FirstName", "A"),
           new XElement("LastName", "B")),
           new XElement("Book",
            new XAttribute("type", "Author"),
           new XElement("FirstName", "C"),
           new XElement("LastName", "D"))));

        IEnumerable<XElement> elements = xDocument.Element("Books").Elements("Book");
        foreach (XElement element in elements) {
            Console.WriteLine("Source element: {0} : value = {1}", element.Name, element.Value);
        }
        foreach (XAttribute attribute in elements.Attributes()) {
            Console.WriteLine("Attribute: {0} : value = {1}",
             attribute.Name, attribute.Value);
        }

    }
}

   
  








Related examples in the same category

1.Get Attribute with namespace
2.Accessing an Element's Attributes Using the Attributes Method
3.Calling the Remove
4.Accessing an Element's First Attribute with the FirstAttribute Property
5.Accessing the Next Attribute with the NextAttribute Property
6.Accessing the Previous Attribute with the PreviousAttribute Property
7.Accessing the Last Attribute with the LastAttribute Property
8.Accessing an Attribute with the Attribute Method
9.Accessing All of an Element's Attributes with the Attributes Method
10.Removing an Attribute
11.Removing All of an Element's Attributes
12.Changing an Attribute's Value
13.Using SetAttributeValue to Add, Delete, and Update Attributes
14.Calling the Second Attributes Prototype
15.XElement.Attribute returns the XAttribute of this XElement that has the specified XName.
16.XElement.Attributes returns a collection of attributes of this element.
17.Returns a filtered collection of attributes of this element.
18.XElement.FirstAttribute gets the first attribute of this element.
19.XElement.HasAttributes gets a value indicating whether this element as at least one attribute.
20.XElement.LastAttribute gets the last attribute of this element.
21.XElement.RemoveAll removes nodes and attributes from this XElement.
22.XElement.RemoveAttributes removes the attributes of this XElement.
23.XElement.ReplaceAll replaces the child nodes and the attributes of this element
24.XElement.ReplaceAttributes replaces the attributes of this element with the specified content.
25.XElement.SetAttributeValue sets the value of an attribute, adds an attribute, or removes an attribute.