2011-02-21 15 views
93

tengo este código:¿Cómo poner atributos a través de XElement

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. 

cómo poner atributos a Conn? Quiero poner estos atributos que marqué como comentarios, pero si intento establecer los atributos en Conn después de definir EcnAdminConf, no están visibe ... Por lo tanto, quiero configurarlos de alguna manera para que el XML empiece a verse así:

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

Respuesta

202

Añadir XAttribute en el constructor de la XElement, como

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

también puede agregar varios atributos o elementos a través de la constructora

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

o puede usar el método Add-Method del XElement para agregar atributos

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