How to use XAttribute to hold name/value pair associated with an XML element in C#

1 Answer

0 votes
using System;
using System.Xml.Linq;
 
class Program
{
    static void Main() {
        XElement ex = new XElement("Data");

        XAttribute attrib = new XAttribute("CSharp", "12345");
        
        Console.WriteLine(attrib);
        
        int i = (int)attrib;
        Console.WriteLine(i);
        
        string s = (string)attrib;
        Console.WriteLine(s);
        
        ex.Add(attrib);
        Console.WriteLine(ex);
        
        Console.WriteLine(ex.Attribute("CSharp")?.Value);
    }
}

 
 
/*
run:

CSharp="12345"
12345
12345
<Data CSharp="12345" />
12345
  
*/

 



answered Jun 27, 2024 by avibootz

Related questions

2 answers 211 views
1 answer 288 views
1 answer 316 views
1 answer 225 views
2 answers 240 views
240 views asked Aug 27, 2023 by avibootz
...