Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put attributes via XElement

People also ask

How to add attribute to XElement in c#?

XElement myelement = new XElement("myelement"); myelement. Add(new XAttribute("attributename", "attributevalue"); Console. WriteLine(myelement);

How do I get XElement attribute value?

Value: If you are getting the value and the attribute might not exist, it is more convenient to use the explicit conversion operators, and assign the attribute to a nullable type such as string or Nullable<T> of Int32 . If the attribute does not exist, then the nullable type is set to null.

What is XElement C#?

The XElement class represents an XML element in XLinq. The code creaes a root node called Authors and adds children Author nodes. The XAttribute class represents an attribute of an element. XElement. Save method saves the contents of XElement to a XML file.


Add XAttribute in the constructor of the XElement, like

new XElement("Conn", new XAttribute("Server", comboBox1.Text));

You can also add multiple attributes or elements via the constructor

new XElement("Conn", new XAttribute("Server", comboBox1.Text), new XAttribute("Database", combobox2.Text));

or you can use the Add-Method of the XElement to add attributes

XElement element = new XElement("Conn");
XAttribute attribute = new XAttribute("Server", comboBox1.Text);
element.Add(attribute);