Cast attribute and check null in CSharp

Description

The following code shows how to cast attribute and check null.

Example


using System;//ww  w.ja v a  2 s  .  c om
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;

public class MainClass{
   public static void Main(){
        XElement root = new XElement("Root",
            new XAttribute("Att1", "attribute 1 content"),
            new XAttribute("Att2", "2")
        );
        string c1 = (string)root.Attribute("Att1");
        Console.WriteLine("c1:{0}", c1 == null ? "attribute does not exist" : c1);
        
    }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style