Dominating Dominating - 9 months ago 56
C# Question

How to put attributes via XElement

I have this code:

XElement EcnAdminConf = new XElement("Type",
new XElement("Connections",
new XElement("Conn"),
// Conn.SetAttributeValue("Server", comboBox1.Text);
//Conn.SetAttributeValue("DataBase", comboBox2.Text))),
new XElement("UDLFiles")));

how to put attributes to Conn? I want to put this attributes which I marked as comments, but if I try to set the attributes to Conn after defining
they are not visibe... So I want to set them somehow so the XML begin to look like this:

<Conn ServerName="FAXSERVER\SQLEXPRESS" DataBase="SPM_483000" />
<Conn ServerName="FAXSERVER\SQLEXPRESS" DataBase="SPM_483000" />
<UDLFiles />


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);