C#在xml文件中指定节点插入节点和属性

C#在xml文件中指定节点插入节点和属性,第1张

      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)

}

public void InsertXml()

{

string path="Xml文件的位置"

XmlDocument doc=new XmlDocument()

doc.Load(path)

XmlNode root=doc.DocumentElement//获得根节点

foreach(XmlNode items in root.ChildNodes)

{

if(items.Name=="data")

{

foreach(XmlNode item in items.ChildNodes)

{

if(item.Name="meter")

{

XmlNode xm1=doc.CreateElement("function")

function.SetAttribute("id","06")

function.SetAttribute("coding","A")

function.SetAttribute("error","A")

function.InnerText="223.33"

item.AppendChild(function)

doc.Save(path)

return

}

}

}

}

}

参考下吧


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

原文地址:https://54852.com/bake/11640356.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存