How to put attributes through 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"))); //Conn. 

How to set attributes in Conn? I want to put these attributes that are marked as comments, but if I try to set the Conn attributes after defining EcnAdminConf , they will not be visibe ... Therefore, I want to somehow set them so that the XML starts to look like this:

  <Type> <Connections> <Conn ServerName="FAXSERVER\SQLEXPRESS" DataBase="SPM_483000" /> <Conn ServerName="FAXSERVER\SQLEXPRESS" DataBase="SPM_483000" /> </Connections> <UDLFiles /> </Type> 
+121
c # xml linq-to-xml
Feb 21 '11 at 8:55
source share
1 answer

Add an XAttribute to the XElement constructor, e.g.

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

You can also add multiple attributes or elements through the constructor.

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

or you can use Add-Method for XElement to add attributes

 XElement element = new XElement("Conn"); XAttribute attribute = new XAttribute("Server", comboBox1.Text); element.Add(attribute); 
+246
Feb 21 2018-11-11T00:
source share



All Articles