XElement el = XElement.Parse(xml)
XElement newEl = new XElement("image", "d:/1.jpg", new XAttribute("name", "1.jpg"))
el.Elements("Background").Where(e=>e.Attribute("FileName").Value=="AAA").First().Add(newEl)
Console.WriteLine(el)
这个应该是Linq to Xml吧,Linq to Sql是数据库的
帮你写好了,截图和核心代码我发在这里,整个项目我放在附件里,如有疑问欢迎追问。
核心代码:
public static void AppendContent(ContentData contentData){
// Load xml file into memory
var xml = new XmlDocument()
xml.Load("MyXml.xml")
// select the root of the xml file
var root = xml.SelectSingleNode("NewDataSet")
// create content data node
var contentDataNode = xml.CreateElement("ContentData")
// create content data node, and assign the corresponding value
var idNode = xml.CreateElement("ID")
idNode.InnerText = contentData.ID.ToString()
// create content data node, and assign the corresponding value
var contentNode = xml.CreateElement("Content")
contentNode.InnerText = contentData.Content
// create content data node, and assign the corresponding value
var parentIdNode = xml.CreateElement("ParentID")
parentIdNode.InnerText = contentData.ParentID.ToString()
// append three child nodes to the parent node(contentDataNode)
contentDataNode.AppendChild(idNode)
contentDataNode.AppendChild(contentNode)
contentDataNode.AppendChild(parentIdNode)
// append contentDataNode to the root
root.AppendChild(contentDataNode)
// save the file
xml.Save("MyXml.xml")
}
关键截图:
运行程序添加节点:
添加节点后:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)