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

C#怎么向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"))

这样看看

      private void button1_Click(object sender, EventArgs e)

        {

            string File = @"c:\TableColumns.xml"

            System.Xml.XmlDocument xmlDoc=new System.Xml.XmlDocument()

            xmlDoc.Load(File)

            System.Xml.XmlNode xn = xmlDoc.DocumentElement.SelectSingleNode("TableColumns/TableColumn[@id=\"IndexUnit\"]")//读取id=IndexUnit的TableColumn节点,在xml教材中的xPath章节专门介绍这种查询方法。

            System.Xml.XmlElement vs=xmlDoc.CreateElement("Visibility")//创建新节点

            System.Xml.XmlElement Hd = xmlDoc.CreateElement("Hidden")

            Hd.InnerText = "true"

            vs.AppendChild(Hd)//添加为子节点

            xn.AppendChild(vs)

            xmlDoc.Save(File)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11275345.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存