C#怎么向xml文件添加多条内容?
例如:我这里有2个xml文件,我想把他放在一起,怎么写?要完整的添加过程代码。保存的路径就先写在c盘,文件名为1.xml。文件1:-<SYSTEM_CONFIG><DB_...
例如:我这里有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", "false");
xe2.SetAttributeValue("User_ID", "sa");
xe2.SetAttributeValue("Password", "");
xe2.SetAttributeValue("Catalog", "Lt_SQL");
xe2.SetAttributeValue("Data_Source", "LT");
xe2.SetAttributeValue("Table_Name", "tbAccessNodeAttr");
xd1.Save(@"C:\1.xml");
XDocument xd = XDocument.Load(@"C:\1.xml");
XElement xBoot = xd.Root;
XElement xe2 = new XElement("ODBCconfig");
xBoot.Add(xe2);
xe2.SetAttributeValue("DSN", "testSybase");
xe2.SetAttributeValue("Description","");
xe2.SetAttributeValue("SeverName", "LT");
xe2.SetAttributeValue("SeverPort", "5000");
xe2.SetAttributeValue("DatabaseName","master");
xe2.SetAttributeValue("LogonID", "sa");
xd.Save(@"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", "false");
xe2.SetAttributeValue("User_ID", "sa");
xe2.SetAttributeValue("Password", "");
xe2.SetAttributeValue("Catalog", "Lt_SQL");
xe2.SetAttributeValue("Data_Source", "LT");
xe2.SetAttributeValue("Table_Name", "tbAccessNodeAttr");
xd1.Save(@"C:\1.xml");
XDocument xd = XDocument.Load(@"C:\1.xml");
XElement xBoot = xd.Root;
XElement xe2 = new XElement("ODBCconfig");
xBoot.Add(xe2);
xe2.SetAttributeValue("DSN", "testSybase");
xe2.SetAttributeValue("Description","");
xe2.SetAttributeValue("SeverName", "LT");
xe2.SetAttributeValue("SeverPort", "5000");
xe2.SetAttributeValue("DatabaseName","master");
xe2.SetAttributeValue("LogonID", "sa");
xd.Save(@"c:\1.xml");
你给的方法我再试下,呵呵,谢谢你了 展开
1个回答
展开全部
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);
RootElement.Add(element);
xDoc.Save(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);
RootElement.Add(element);
xDoc.Save(Server.MapPath("XMLFile1.xml"));
这样看看
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询