Creating an Attribute and Adding It to Its Element : XAttribute « XML LINQ « C# / C Sharp






Creating an Attribute and Adding It to Its Element

  

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() {

        XElement xBookParticipant = new XElement("BookParticipant"); 
        XAttribute xAttribute = new XAttribute("type", "Author"); 
        xBookParticipant.Add(xAttribute);
        Console.WriteLine(xBookParticipant);
    }
}

   
  








Related examples in the same category

1.Using the LINQ to XML API to Create an XML Document
2.Creating an Attribute with Functional Construction
3.XAttribute Class represents an XML attribute.
4.Create a new instance of the XAttribute class from the specified name and value.
5.Create XAttribute with namespace
6.Add XAttribute array to XElement
7.XAttribute.IsNamespaceDeclaration Property tells if this attribute is a namespace declaration.
8.XAttribute.Name Property returns the expanded name of this attribute.
9.XAttribute.NextAttribute Property returns the next attribute of the parent element.
10.XAttribute.NodeType Property returns the node type for this node.
11.XAttribute.PreviousAttribute Property returns the previous attribute of the parent element.
12.XAttribute.Remove removes this attribute from its parent element.
13.XAttribute.SetValue sets the value of this attribute.
14.XAttribute.ToString converts the current XAttribute object to a string representation.
15.XAttribute.Value gets or sets the value of this attribute.
16.It is easier to use casting when the attribute might or might not exist.
17.Cast the value of this XAttribute to a Boolean.
18.Cast the value of this XAttribute to a DateTime.