XDocument怎么实现追加xml文件,而不覆盖原文件内容。C#

XDocument怎么实现追加xml文件,而不覆盖原文件内容。C#,第1张

=======================bookstore.XML内容====================

<?xml version="1.0" encoding="gb2312"?>

<bookstore>

  <book>

  </book>

  <book>

  </book>

  <book genre="李赞红" ISBN="2-3631-4">

    <title>CS从入门到精通</title>

    <author>候捷</author>

    <price>58.3</price>

  </book>

  <book genre="李赞红" ISBN="2-3631-4">

    <title>CS从入门到精通</title>

    <author>候捷</author>

    <price>58.3</price>

  </book>

</bookstore>

=======================读取========================

            XmlDocument doc = new XmlDocument()

            doc.Load(@"d:\bookstore.xml")

            XmlElement root = doc.DocumentElement

MessageBox.Show(root.SelectNodes("book")[0].InnerText)

添加应该是你想要实现的主要功能,附加了其他功能,可以参考一下。

=======================添加=========================

            XmlDocument xmlDoc = new XmlDocument()

            xmlDoc.Load(@"d:\bookstore.xml")

            XmlNode root = xmlDoc.SelectSingleNode("bookstore")//查找<bookstore>

            XmlElement xe1 = xmlDoc.CreateElement("book")//创建一个<book>节点

            xe1.SetAttribute("genre", "李赞红")//设置该节点的genre属性

            xe1.SetAttribute("ISBN", "2-3631-4")//设置该节点的ISBN属性

            XmlElement xesub1 = xmlDoc.CreateElement("title")//添加一个名字为title的子节点

            xesub1.InnerText = "CS从入门到精通"//设置文本

            xe1.AppendChild(xesub1)//把title添加到<book>节点中

            XmlElement xesub2 = xmlDoc.CreateElement("author")

            xesub2.InnerText = "候捷"

            xe1.AppendChild(xesub2)

            XmlElement xesub3 = xmlDoc.CreateElement("price")

            xesub3.InnerText = "58.3"

            xe1.AppendChild(xesub3)

            root.AppendChild(xe1)//把book添加到<bookstore>根节点中

            xmlDoc.Save(@"d:\bookstore.xml")

======================修改========================

            XmlDocument xmlDoc = new XmlDocument()

            xmlDoc.Load(@"d:\bookstore.xml")

            XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes//获取bookstore节点的所有子节点

            foreach (XmlNode xn in nodeList)//遍历所有名字为bookstore的子节点

            {

                XmlElement xe = (XmlElement)xn//将子节点类型转换为XmlElement类型

                if (xe.GetAttribute("genre") == "李赞红")//如果genre属性值为“李赞红”

                {

                    xe.SetAttribute("genre", "update李赞红")//则修改该属性为“update李赞红”

                    XmlNodeList nls = xe.ChildNodes//继续获取xe(xn)子节点的所有子节点

                    foreach (XmlNode xn1 in nls)//遍历

                    {

                        XmlElement xe2 = (XmlElement)xn1//转换类型

                        if (xe2.Name == "author")//如果找到

                        {

                            xe2.InnerText = "亚胜"//则修改

                            break//找到退出来

                        }

                    }

                    break

                }

            }

            xmlDoc.Save(@"d:\bookstore.xml")//保存。

========================删除=========================

            XmlDocument xmlDoc = new XmlDocument()

            xmlDoc.Load(@"d:\bookstore.xml")

            XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes

            foreach (XmlNode xn in xnl)

            {

                XmlElement xe = (XmlElement)xn

if (xe.GetAttribute("genre") == "fantasy")

                {

                    xe.RemoveAttribute("genre")//删除genre属性

                }

                else if (xe.GetAttribute("genre") == "update李赞红")

                {

                    xe.RemoveAll()//删除该节点的全部内容

                }

            }

            xmlDoc.Save(@"d:\bookstore.xml")

Treeview控件Name设为tvXML ,然后利用递归循环调用

private void Form1_Load(object sender, EventArgs e)

{

XDocument doc = XDocument.Load("XMLFile1.xml")

XElement xRoot = doc.Root

TreeNode node = tvXML.Nodes.Add(xRoot.Name.ToString())

node.Tag = xRoot

AddTreeNodes(node)

}

private void AddTreeNodes(TreeNode node)

{

XElement xRoot = (XElement)node.Tag

IEnumerable<XElement>xIEnumers = xRoot.Elements()

foreach (var item in xIEnumers)

{

XName name = item.Name

TreeNode tn1 = node.Nodes.Add(item.Name.ToString())

tn1.Tag = item

AddTreeNodes(tn1)

}

}

C# *** 作XML 有以下几种方式:

1:使用XmlDocument相关类库和方法 *** 作xml

2:使用XDocument相关类库和方法 *** 作xml

3:使用XmlReader和XmlWriter相关类库和方法 *** 作xml

获得指定节点的值也需要 分为属性和元素

1:使用XmlDocument

XmlDocument doc = new XmlDocument()

doc.Load("Customer2.xml")

// XmlNodeList nodeList = doc.GetElementsByTagName("row")

XmlNodeList nodeList = doc.SelectNodes("/Table/row")

//读取属性+元素

foreach (XmlNode item in nodeList)

{

customerInfo.AppId = item.Attributes["AppID"].Value

customerInfo.CustomerID = item["CustomerID"].InnerText

}

2:使用XDocument

XDocument xdoc = XDocument.Load("Customer2.xml")

var custs = from customer in xdoc.Descendants("row")

select new

{//读取属性+元素

Version = customer.Attribute("Version").Value,

CustomerID = customer.Element("CustomerID").Value,

}

3:使用XmlReader

XmlReader reader = XmlReader.Create("Customer2.xml", settings)

//读取属性

while (reader.Read())

{

if (reader.NodeType == XmlNodeType.Element)

{

switch (reader.Name)

{

case "row":

customerInfo = new CustomerInfo()

if (reader.HasAttributes)

{

customerInfo.AppId = reader.GetAttribute("AppID")

customerInfo.Version = reader.GetAttribute("Version")

}

break

case "CustomerID":

customerInfo.CustomerID = reader.ReadString()

break

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存