如何通过的XElement把属性 [英] How to put attributes via XElement

查看:112
本文介绍了如何通过的XElement把属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

 的XElement EcnAdminConf =新的XElement(类型,
新的XElement(连接,
新的XElement(康涅狄格州),
// Conn.SetAttributeValue(服务器,comboBox1.Text);
//Conn.SetAttributeValue(\"DataBase,comboBox2。文本))),
新的XElement(UDLFiles)));
//康涅狄格州。



如何把属性康涅狄格州?我想把这个属性,这标志着我作为注释,但如果我尝试定义后,设置属性康涅狄格州 EcnAdminConf 他们没有visibe ...所以我要定他们以某种方式让XML开始看起来是这样的:

 <类型> 
<连接GT&;
<康涅狄格服务器名称=FAXSERVER\SQLEXPRESS数据库=SPM_483000/>
<康涅狄格服务器名称=FAXSERVER\SQLEXPRESS数据库=SPM_483000/>
< /连接>
< UDLFiles />
< /型号>


解决方案

添加 XAttribute 的XElement ,像



的构造函数

 新的XElement(康涅狄格州,新的XAttribute(服务器,comboBox1.Text)); 

您还可以通过构造


$ B添加多个属性或元素$ b

 新的XElement(康涅狄格州,新的XAttribute(服务器,comboBox1.Text),新XAttribute(数据库,combobox2.Text)); 



也可以使用的XElement 添加属性。

 的XElement元素=新的XElement(康恩); 
XAttribute属性=新XAttribute(服务器,comboBox1.Text);
element.Add(属性);


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 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 EcnAdminConf they are not visibe... So I want to set them somehow so the XML begin to look like this:

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

解决方案

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

这篇关于如何通过的XElement把属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆