C#怎么向xml文件添加多条内容?

来源:百度知道 编辑:UC知道 时间:2024/06/28 13:23:34
例如:我这里有2个xml文件,我想把他放在一起,怎么写?要完整的添加过程代码。保存的路径就先写在c盘,文件名为1.xml。
文件1:
- <SYSTEM_CONFIG>
<DB_CONFIG Provider="SQLOleDb.1" Persist_Security_Info="false" User_ID="sa" Password="" Catalog="Lt_SQL" Data_Source="LT" Table_Name="tbAccessNodeAttr" />
</SYSTEM_CONFIG>
文件2:
- <SYSTEM_CONFIG>
<ODBCconfig DataSourceName="testSybase" Description="" SeverName="LT" SeverPort="5000" DatabaseName="master" LogonID="sa" />
</SYSTEM_CONFIG>
写在一起怎么写?
知道怎么弄了:
XDocument xd1 = new XDocument();
XElement xBoot = new XElement("SYSTEM_CONFIG");
xd1.Add(xBoot);
XElement xe2 = new XElement("DB_CONFIG");
xBoot.Add(xe2);
xe2.SetAttributeValue("Provider", "SQLOleDb.1");
xe2.SetAttributeValue("Persist_Security_Info",

System.Xml.Linq.XDocument xDoc = System.Xml.Linq.XDocument.Load(Server.MapPath("XMLFile1.xml"));
System.Xml.Linq.XElement RootElement = xDoc.Element("SYSTEM_CONFIG");

System.Xml.Linq.XElement element = new System.Xml.Linq.XElement("ODBCconfig");
System.Xml.Linq.XAttribute attribute = new System.Xml.Linq.XAttribute("DataSourceName", "testSybase");
element.Add(attribute);

attribute = new System.Xml.Linq.XAttribute("Description", "");
element.Add(attribute);

attribute = new System.Xml.Linq.XAttribute("SeverName", "LT");
element.Add(attribute);

attribute = new System.Xml.Linq.XAttribute("SeverPort", "5000");
element.Add(attribute);